Skip to content

Commit

Permalink
allow setting custom tunnel host
Browse files Browse the repository at this point in the history
  • Loading branch information
aluedeke committed Nov 11, 2024
1 parent 1caa612 commit 636a36f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
15 changes: 14 additions & 1 deletion ios/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func ConnectTUNDevice(remoteIp string, port int, d DeviceEntry) (*net.TCPConn, e
return connectTUN(remoteIp, port)
}

addr, _ := net.ResolveTCPAddr("tcp4", fmt.Sprintf("localhost:%d", d.UserspaceTUNPort))
addr, _ := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", d.UserspaceTUNHost, d.UserspaceTUNPort))
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return nil, fmt.Errorf("ConnectUserSpaceTunnel: failed to dial: %w", err)
Expand Down Expand Up @@ -328,6 +328,9 @@ func connectTUN(address string, port int) (*net.TCPConn, error) {
// 60-105 is leetspeek for go-ios :-D
const defaultHttpApiPort = 60105

// defaultHttpApiHost is the host on which the HTTP-Server runs, by default it is 127.0.0.1
const defaultHttpApiHost = "127.0.0.1"

// DefaultHttpApiPort is the port on which we start the HTTP-Server for exposing started tunnels
// if GO_IOS_AGENT_PORT is set, we use that port. Otherwise we use the default port 60106.
// 60-105 is leetspeek for go-ios :-D
Expand All @@ -338,3 +341,13 @@ func HttpApiPort() int {
}
return port
}

// DefaultHttpApiHost is the host on which the HTTP-Server runs, by default it is 127.0.0.1
// if GO_IOS_AGENT_HOST is set, we use that host. Otherwise we use the default host
func HttpApiHost() string {
host := os.Getenv("GO_IOS_AGENT_HOST")
if host == "" {
return defaultHttpApiHost
}
return host
}
1 change: 1 addition & 0 deletions ios/listdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type DeviceEntry struct {
Address string
Rsd RsdPortProvider
UserspaceTUN bool
UserspaceTUNHost string
UserspaceTUNPort int
}

Expand Down
10 changes: 5 additions & 5 deletions ios/tunnel/tunnel_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ var netClient = &http.Client{
}

func CloseAgent() error {
_, err := netClient.Get(fmt.Sprintf("http://%s:%d/shutdown", "127.0.0.1", ios.HttpApiPort()))
_, err := netClient.Get(fmt.Sprintf("http://%s:%d/shutdown", ios.HttpApiHost(), ios.HttpApiPort()))
if err != nil {
return fmt.Errorf("CloseAgent: failed to send shutdown request: %w", err)
}
return nil
}

func IsAgentRunning() bool {
resp, err := netClient.Get(fmt.Sprintf("http://%s:%d/health", "127.0.0.1", ios.HttpApiPort()))
resp, err := netClient.Get(fmt.Sprintf("http://%s:%d/health", ios.HttpApiHost(), ios.HttpApiPort()))
if err != nil {
return false
}
Expand All @@ -43,7 +43,7 @@ func IsAgentRunning() bool {
func WaitUntilAgentReady() bool {
for {
slog.Info("Waiting for go-ios agent to be ready...")
resp, err := netClient.Get(fmt.Sprintf("http://%s:%d/ready", "127.0.0.1", ios.HttpApiPort()))
resp, err := netClient.Get(fmt.Sprintf("http://%s:%d/ready", ios.HttpApiHost(), ios.HttpApiPort()))
if err != nil {
return false
}
Expand Down Expand Up @@ -155,11 +155,11 @@ func ServeTunnelInfo(tm *TunnelManager, port int) error {
return nil
}

func TunnelInfoForDevice(udid string, tunnelInfoPort int) (Tunnel, error) {
func TunnelInfoForDevice(udid string, tunnelInfoHost string, tunnelInfoPort int) (Tunnel, error) {
c := http.Client{
Timeout: 5 * time.Second,
}
res, err := c.Get(fmt.Sprintf("http://127.0.0.1:%d/tunnel/%s", tunnelInfoPort, udid))
res, err := c.Get(fmt.Sprintf("http://%s:%d/tunnel/%s", tunnelInfoHost, tunnelInfoPort, udid))
if err != nil {
return Tunnel{}, fmt.Errorf("TunnelInfoForDevice: failed to get tunnel info: %w", err)
}
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ The commands work as following:
return
}

tunnelInfoHost, err := arguments.String("--tunnel-info-host")
if err != nil {
tunnelInfoHost = ios.HttpApiHost()
}

tunnelInfoPort, err := arguments.Int("--tunnel-info-port")
if err != nil {
tunnelInfoPort = ios.HttpApiPort()
Expand All @@ -333,6 +338,11 @@ The commands work as following:
udid, _ := arguments.String("--udid")
address, addressErr := arguments.String("--address")
rsdPort, rsdErr := arguments.Int("--rsd-port")
userspaceTunnelHost, userspaceTunnelHostErr := arguments.String("--userspace-host")
if userspaceTunnelHostErr != nil {
userspaceTunnelHost = ios.HttpApiHost()
}

userspaceTunnelPort, userspaceTunnelErr := arguments.Int("--userspace-port")

device, err := ios.GetDevice(udid)
Expand All @@ -342,13 +352,15 @@ The commands work as following:
if addressErr == nil && rsdErr == nil {
if userspaceTunnelErr == nil {
device.UserspaceTUN = true
device.UserspaceTUNHost = userspaceTunnelHost
device.UserspaceTUNPort = userspaceTunnelPort
}
device = deviceWithRsdProvider(device, udid, address, rsdPort)
} else {
info, err := tunnel.TunnelInfoForDevice(device.Properties.SerialNumber, tunnelInfoPort)
info, err := tunnel.TunnelInfoForDevice(device.Properties.SerialNumber, tunnelInfoHost, tunnelInfoPort)
if err == nil {
device.UserspaceTUNPort = info.UserspaceTUNPort
device.UserspaceTUNHost = userspaceTunnelHost
device.UserspaceTUN = info.UserspaceTUN
device = deviceWithRsdProvider(device, udid, info.Address, info.RsdPort)
} else {
Expand Down
2 changes: 1 addition & 1 deletion restapi/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func DeviceMiddleware() gin.HandlerFunc {
return
}

info, err := tunnel.TunnelInfoForDevice(device.Properties.SerialNumber, ios.HttpApiPort())
info, err := tunnel.TunnelInfoForDevice(device.Properties.SerialNumber, ios.HttpApiHost(), ios.HttpApiPort())
if err == nil {
log.WithField("udid", device.Properties.SerialNumber).Printf("Received tunnel info %v", info)

Expand Down

0 comments on commit 636a36f

Please sign in to comment.