-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5526 from cboddy/feat/http_proxy_over_p2p
[http_proxy_over_p2p]
- Loading branch information
Showing
6 changed files
with
440 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package corehttp | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"strings" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
|
||
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol" | ||
p2phttp "gx/ipfs/QmcLYfmHLsaVRKGMZQovwEYhHAjWtRjg1Lij3pnzw5UkRD/go-libp2p-http" | ||
) | ||
|
||
// ProxyOption is an endpoint for proxying a HTTP request to another ipfs peer | ||
func ProxyOption() ServeOption { | ||
return func(ipfsNode *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { | ||
mux.HandleFunc("/p2p/", func(w http.ResponseWriter, request *http.Request) { | ||
// parse request | ||
parsedRequest, err := parseRequest(request) | ||
if err != nil { | ||
handleError(w, "failed to parse request", err, 400) | ||
return | ||
} | ||
|
||
request.Host = "" // Let URL's Host take precedence. | ||
request.URL.Path = parsedRequest.httpPath | ||
target, err := url.Parse(fmt.Sprintf("libp2p://%s", parsedRequest.target)) | ||
if err != nil { | ||
handleError(w, "failed to parse url", err, 400) | ||
return | ||
} | ||
|
||
rt := p2phttp.NewTransport(ipfsNode.PeerHost, p2phttp.ProtocolOption(parsedRequest.name)) | ||
proxy := httputil.NewSingleHostReverseProxy(target) | ||
proxy.Transport = rt | ||
proxy.ServeHTTP(w, request) | ||
}) | ||
return mux, nil | ||
} | ||
} | ||
|
||
type proxyRequest struct { | ||
target string | ||
name protocol.ID | ||
httpPath string // path to send to the proxy-host | ||
} | ||
|
||
// from the url path parse the peer-ID, name and http path | ||
// /p2p/$peer_id/http/$http_path | ||
// or | ||
// /p2p/$peer_id/x/$protocol/http/$http_path | ||
func parseRequest(request *http.Request) (*proxyRequest, error) { | ||
path := request.URL.Path | ||
|
||
split := strings.SplitN(path, "/", 5) | ||
if len(split) < 5 { | ||
return nil, fmt.Errorf("Invalid request path '%s'", path) | ||
} | ||
|
||
if split[3] == "http" { | ||
return &proxyRequest{split[2], protocol.ID("/http"), split[4]}, nil | ||
} | ||
|
||
split = strings.SplitN(path, "/", 7) | ||
if split[3] != "x" || split[5] != "http" { | ||
return nil, fmt.Errorf("Invalid request path '%s'", path) | ||
} | ||
|
||
return &proxyRequest{split[2], protocol.ID("/x/" + split[4] + "/http"), split[6]}, nil | ||
} | ||
|
||
func handleError(w http.ResponseWriter, msg string, err error, code int) { | ||
w.WriteHeader(code) | ||
fmt.Fprintf(w, "%s: %s\n", msg, err) | ||
log.Warningf("http proxy error: %s: %s", err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package corehttp | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ipfs/go-ipfs/thirdparty/assert" | ||
|
||
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol" | ||
) | ||
|
||
type TestCase struct { | ||
urlprefix string | ||
target string | ||
name string | ||
path string | ||
} | ||
|
||
var validtestCases = []TestCase{ | ||
{"http://localhost:5001", "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", "/http", "path/to/index.txt"}, | ||
{"http://localhost:5001", "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", "/x/custom/http", "path/to/index.txt"}, | ||
{"http://localhost:5001", "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", "/x/custom/http", "http/path/to/index.txt"}, | ||
} | ||
|
||
func TestParseRequest(t *testing.T) { | ||
for _, tc := range validtestCases { | ||
url := tc.urlprefix + "/p2p/" + tc.target + tc.name + "/" + tc.path | ||
req, _ := http.NewRequest("GET", url, strings.NewReader("")) | ||
|
||
parsed, err := parseRequest(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.True(parsed.httpPath == tc.path, t, "proxy request path") | ||
assert.True(parsed.name == protocol.ID(tc.name), t, "proxy request name") | ||
assert.True(parsed.target == tc.target, t, "proxy request peer-id") | ||
} | ||
} | ||
|
||
var invalidtestCases = []string{ | ||
"http://localhost:5001/p2p/http/foobar", | ||
"http://localhost:5001/p2p/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/x/custom/foobar", | ||
} | ||
|
||
func TestParseRequestInvalidPath(t *testing.T) { | ||
for _, tc := range invalidtestCases { | ||
url := tc | ||
req, _ := http.NewRequest("GET", url, strings.NewReader("")) | ||
|
||
_, err := parseRequest(req) | ||
if err == nil { | ||
t.Fail() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.