8000 feat(client): Add address parsing functionality and support custom ports by andrewbytecoder · Pull Request #75 · melbahja/goph · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(client): Add address parsing functionality and support custom ports #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions client.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net"
"os"
"strings"
"time"

"github.com/pkg/sftp"
Expand All @@ -35,6 +36,27 @@ type Config struct {
// DefaultTimeout is the timeout of ssh client connection.
var DefaultTimeout = 20 * time.Second

// parseAddress 解析地址字符串,返回主机和端口
func parseAddress(addr string) (host string, port int, err error) {
// 分割地址和端口
if strings.Contains(addr, ":") {
host, portStr, splitErr := net.SplitHostPort(addr)
if splitErr != nil {
return "", 0, fmt.Errorf("invalid address format: %v", splitErr)
}

// 尝试将端口转换为整数
port, convErr := net.LookupPort("tcp", portStr)
if convErr != nil {
return "", 0, fmt.Errorf("invalid port format: %v", convErr)
}
return host, port, nil
}

// 如果没有端口,返回主机名/IP 和默认端口 0
return addr, 0, nil
}

// New starts a new ssh connection, the host public key must be in known hosts.
func New(user string, addr string, auth Auth) (c *Client, err error) {

Expand All @@ -44,10 +66,21 @@ func New(user string, addr string, auth Auth) (c *Client, err error) {
return
}

// Parse addr to extract IP and port
host, port, err := parseAddress("127.0.0.1")
if err != nil {
fmt.Println("Failed to parse address:", err)
return
}
// If no port is specified, default to 22
if port == 0 {
port = 22
}

c, err = NewConn(&Config{
User: user,
Addr: addr,
Port: 22,
Addr: host,
Port: uint(port),
Auth: auth,
Timeout: DefaultTimeout,
Callback: callback,
Expand All @@ -60,10 +93,20 @@ func New(user string, addr string, auth Auth) (c *Client, err error) {
// if there a "man in the middle proxy", this can harm you!
// You can add the key to know hosts and use New() func instead!
func NewUnknown(user string, addr string, auth Auth) (*Client, error) {
// Parse addr to extract IP and port
host, port, err := parseAddress("127.0.0.1")
if err != nil {
return nil, err
}
// If no port is specified, default to 22
if port == 0 {
port = 22
}

return NewConn(&Config{
User: user,
Addr: addr,
Port: 22,
Addr: host,
Port: uint(port),
Auth: auth,
Timeout: DefaultTimeout,
Callback: ssh.InsecureIgnoreHostKey(),
Expand Down
0