Skip to content

Commit

Permalink
Merge pull request #11141 from filecoin-project/fix/worker-IPv6
Browse files Browse the repository at this point in the history
fix: worker: Support IPv6 formatted API-keys
  • Loading branch information
magik6k authored Aug 16, 2023
2 parents c46aea6 + 94e48ae commit 22f4901
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions cmd/lotus-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/google/uuid"
"github.com/ipfs/go-datastore/namespace"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/urfave/cli/v2"
"go.opencensus.io/stats/view"
Expand Down Expand Up @@ -530,9 +531,14 @@ var runCmd = &cli.Command{

log.Info("Opening local storage; connecting to master")
const unspecifiedAddress = "0.0.0.0"

address := cctx.String("listen")
addressSlice := strings.Split(address, ":")
if ip := net.ParseIP(addressSlice[0]); ip != nil {
host, port, err := net.SplitHostPort(address)
if err != nil {
return err
}

if ip := net.ParseIP(host); ip != nil {
if ip.String() == unspecifiedAddress {
timeout, err := time.ParseDuration(cctx.String("timeout"))
if err != nil {
Expand All @@ -542,11 +548,21 @@ var runCmd = &cli.Command{
if err != nil {
return err
}
address = rip + ":" + addressSlice[1]
host = rip
}
}

localStore, err := paths.NewLocal(ctx, lr, nodeApi, []string{"http://" + address + "/remote"})
var newAddress string

// Check if the IP address is IPv6
ip := net.ParseIP(host)
if ip.To4() == nil && ip.To16() != nil {
newAddress = "[" + host + "]:" + port
} else {
newAddress = host + ":" + port
}

localStore, err := paths.NewLocal(ctx, lr, nodeApi, []string{"http://" + newAddress + "/remote"})
if err != nil {
return err
}
Expand Down Expand Up @@ -740,21 +756,48 @@ func extractRoutableIP(timeout time.Duration) (string, error) {
deprecatedMinerMultiAddrKey := "STORAGE_API_INFO"
env, ok := os.LookupEnv(minerMultiAddrKey)
if !ok {
// TODO remove after deprecation period
_, ok = os.LookupEnv(deprecatedMinerMultiAddrKey)
if ok {
log.Warnf("Using a deprecated env(%s) value, please use env(%s) instead.", deprecatedMinerMultiAddrKey, minerMultiAddrKey)
}
return "", xerrors.New("MINER_API_INFO environment variable required to extract IP")
}
minerAddr := strings.Split(env, "/")
conn, err := net.DialTimeout("tcp", minerAddr[2]+":"+minerAddr[4], timeout)

// Splitting the env to separate the JWT from the multiaddress
splitEnv := strings.SplitN(env, ":", 2)
if len(splitEnv) < 2 {
return "", xerrors.Errorf("invalid MINER_API_INFO format")
}
// Only take the multiaddress part
maddrStr := splitEnv[1]

maddr, err := multiaddr.NewMultiaddr(maddrStr)
if err != nil {
return "", err
}
defer conn.Close() //nolint:errcheck

localAddr := conn.LocalAddr().(*net.TCPAddr)
minerIP, _ := maddr.ValueForProtocol(multiaddr.P_IP6)
minerPort, _ := maddr.ValueForProtocol(multiaddr.P_TCP)

// Check if the IP is IPv6 and format the address appropriately
var addressToDial string
if ip := net.ParseIP(minerIP); ip.To4() == nil && ip.To16() != nil {
addressToDial = "[" + minerIP + "]:" + minerPort
} else {
addressToDial = minerIP + ":" + minerPort
}

conn, err := net.DialTimeout("tcp", addressToDial, timeout)
if err != nil {
return "", err
}

return strings.Split(localAddr.IP.String(), ":")[0], nil
defer func() {
if cerr := conn.Close(); cerr != nil {
log.Errorf("Error closing connection: %v", cerr)
}
}()

localAddr := conn.LocalAddr().(*net.TCPAddr)
return localAddr.IP.String(), nil
}

0 comments on commit 22f4901

Please sign in to comment.