Skip to content

Commit

Permalink
Some code reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
mcamou committed Dec 3, 2024
1 parent 3c192fe commit 6f29462
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions pkg/network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@ import (
// number is ignored), it then establishes a Libp2p connection to the target host, which
// then connects to its localhost:TargetPort.
//
// TODO: Move this to docs or someplace else
//
// To use it:
// * You must run the Cosmos node and the Masa node side by side
// * In the OS:
// * Add the Masa node PeerID as a localhost alias in /etc/hosts (Linux/MacOS) or XXXXX (Windows) (Cosmos checks that its own name resolves)
// * In the Cosmos node:
// * Set the environment variable `HTTP_PROXY=127.0.0.1:<proxyListenPort>
// * Add to the config: `external_address = <Masa node PeerID>:0` (the port number is ignored)
// * Set the following in the `[p2p]` section of config.toml with Ignite, it's at `$HOME/.bobtestchain/config`:
// * `external_address = <Masa node PeerID>:0` (the port number is ignored)
// * `seeds = <Masa node PeerID of the bootstrappers:0>`
// * You might also want to set some `persistent_peers`
// * In the Masa node:
// * Set the following configuration parameters:
// proxy_enabled = true
// proxyListenAddr = <IP address for the proxy to listen on>
// proxyListenPort = <Port number for the proxy to listen on>
// proxyTargetPort = <Port number that the Cosmos node is listening on>
// * You will need to start up the Masa nodes BEFORE the Cosmos nodes, and the bootstrappers before the other nodes

type Proxy struct {
host host.Host
Expand Down Expand Up @@ -75,6 +83,22 @@ func (px *Proxy) handleTunnel(ctx context.Context, w http.ResponseWriter, req *h
}

Check warning on line 83 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L80-L83

Added lines #L80 - L83 were not covered by tests

logrus.Debugf("Received CONNECT request %#v", req)

logrus.Debug("Hijacking the connection")
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
logrus.Error("Hijacking not supported")
return
}

Check warning on line 93 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L90-L93

Added lines #L90 - L93 were not covered by tests

clientConn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
logrus.Errorf("Error while hijacking: %v", err)
return
}

Check warning on line 100 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L97-L100

Added lines #L97 - L100 were not covered by tests

parts := strings.Split(req.RequestURI, ":")
peerID, err := peer.Decode(parts[0])
if err != nil {
Expand All @@ -98,21 +122,6 @@ func (px *Proxy) handleTunnel(ctx context.Context, w http.ResponseWriter, req *h
return
}

Check warning on line 123 in pkg/network/proxy.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/proxy.go#L120-L123

Added lines #L120 - L123 were not covered by tests

logrus.Debug("Stream established, hijacking")
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
logrus.Error("Hijacking not supported")
return
}

clientConn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
logrus.Errorf("Error while hijacking: %v", err)
return
}

logrus.Debug("Sending response header")
hdr := fmt.Sprintf("%s 200 OK\n\n", req.Proto)
_, err = clientConn.Write([]byte(hdr))
Expand Down

0 comments on commit 6f29462

Please sign in to comment.