-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d0f6d3
commit 6e3f8da
Showing
1 changed file
with
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tailscale | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const LoftTSNetDomain = "ts.loft" | ||
|
||
func GetHostname() (string, error) { | ||
osHostname, err := os.Hostname() | ||
if err != nil { | ||
fmt.Printf("Failed to get hostname: %v\n", err) | ||
return "", err | ||
} | ||
osHostname = strings.ToLower(strings.ReplaceAll(osHostname, ".", "-")) | ||
return fmt.Sprintf("devpod.%v.client", osHostname), nil | ||
} | ||
|
||
func GetURL(host string, port int) string { | ||
if port == 0 { | ||
return fmt.Sprintf("%s.%s", host, LoftTSNetDomain) | ||
} | ||
return fmt.Sprintf("%s.%s:%d", host, LoftTSNetDomain, port) | ||
} | ||
|
||
func DirectTunnel(ctx context.Context, network TSNet, host string, port int, stdin io.Reader, stdout io.Writer) error { | ||
address := fmt.Sprintf("%s.%s:%d", host, LoftTSNetDomain, port) | ||
conn, err := network.Dial(ctx, "tcp", address) | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to SSH server in proxy mode: %w", err) | ||
} | ||
defer conn.Close() | ||
|
||
// Forward stdin, stdout, and stderr for proxy mode | ||
go func() { | ||
_, _ = io.Copy(conn, stdin) | ||
}() | ||
go func() { | ||
_, _ = io.Copy(stdout, conn) | ||
}() | ||
|
||
// Block until the connection is closed | ||
<-ctx.Done() | ||
return nil | ||
} |