Skip to content

Commit

Permalink
Merge pull request #3 from gofrp/fix_ci
Browse files Browse the repository at this point in the history
fix: bug
blizard863 authored Apr 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 1d9fd95 + bd47ab0 commit 33684de
Showing 5 changed files with 63 additions and 26 deletions.
8 changes: 6 additions & 2 deletions cmd/go_ssh/main.go
Original file line number Diff line number Diff line change
@@ -56,9 +56,13 @@ func main() {

log.Infof("start to run: %v", cmd)

tc := gssh.NewTunnelClient(cmd.LocalAddr, cmd.ServerAddr, cmd.SSHExtraCmd)
tc, err := gssh.NewTunnelClient(cmd.LocalAddr, cmd.ServerAddr, cmd.SSHExtraCmd)
if err != nil {
log.Errorf("new ssh tunnel client error: %v", err)
return
}

err := tc.Start()
err = tc.Start()
if err != nil {
log.Errorf("cmd: %v run error: %v", cmd, err)
return
6 changes: 0 additions & 6 deletions pkg/config/load.go
Original file line number Diff line number Diff line change
@@ -74,13 +74,9 @@ func LoadConfigure(b []byte, c any, strict bool) error {
defer v1.DisallowUnknownFieldsMu.Unlock()
v1.DisallowUnknownFields = strict

log.Infof("file content: %v", string(b))

var tomlObj interface{}
// Try to unmarshal as TOML first; swallow errors from that (assume it's not valid TOML).
if err := toml.Unmarshal(b, &tomlObj); err == nil {
log.Infof("use toml unmarshal file to struct: toml obj: %v", util.JSONEncode(tomlObj))

b, err = json.Marshal(&tomlObj)
if err != nil {
return err
@@ -98,8 +94,6 @@ func LoadConfigure(b []byte, c any, strict bool) error {

err := decoder.Decode(c)

log.Infof("json decode config: %v", util.JSONEncode(c))

return err
}
return nil
4 changes: 2 additions & 2 deletions pkg/config/parse.go
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ func ParseFRPCConfigToGoSSHParam(

res = append(res, GoSSHParam{
LocalAddr: pv.GetLocalServerAddr(),
ServerAddr: net.JoinHostPort(cfg.ServerAddr, fmt.Sprintf("%d", cfg.SSHServerPort)),
ServerAddr: net.JoinHostPort(cfg.ServerAddr, fmt.Sprintf("%d", cfg.ServerPort)),

SSHExtraCmd: genSSHExtraCmd(*cfg, pv),
})
@@ -82,7 +82,7 @@ func genSSHExtraCmd(c v1.ClientCommonConfig, pc v1.ProxyConfigurer) string {
}

func genDialedCmd(c v1.ClientCommonConfig) string {
return fmt.Sprintf("ssh v0@%v -p %v", c.ServerAddr, c.SSHServerPort)
return fmt.Sprintf("ssh v0@%v -p %v", c.ServerAddr, c.ServerPort)
}

func genReverseCmd(pc v1.ProxyConfigurer) string {
6 changes: 3 additions & 3 deletions pkg/config/v1/client.go
Original file line number Diff line number Diff line change
@@ -40,17 +40,17 @@ type ClientCommonConfig struct {
// default, this value is "0.0.0.0".
ServerAddr string `json:"serverAddr,omitempty"`

// SSHServerPort specifies the port to connect to the ssh server on. By default,
// ServerPort specifies the port to connect to the ssh server on. By default,
// this value is 2200.
SSHServerPort int `json:"sshServerPort,omitempty"`
ServerPort int `json:"serverPort,omitempty"`

// Include other config files for proxies.
IncludeConfigFiles []string `json:"includes,omitempty"`
}

func (c *ClientCommonConfig) Complete() {
c.ServerAddr = util.EmptyOr(c.ServerAddr, "0.0.0.0")
c.SSHServerPort = util.EmptyOr(c.SSHServerPort, 2200)
c.ServerPort = util.EmptyOr(c.ServerPort, 2200)
c.Auth.Complete()
}

65 changes: 52 additions & 13 deletions pkg/gssh/ssh.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gssh

import (
"fmt"
"net"
"os"
"os/user"
"path/filepath"

"golang.org/x/crypto/ssh"

@@ -16,20 +20,58 @@ type TunnelClient struct {

sshConn *ssh.Client
ln net.Listener

authMethod ssh.AuthMethod
}

func NewTunnelClient(localAddr string, sshServer string, commands string) *TunnelClient {
return &TunnelClient{
localAddr: localAddr,
sshServer: sshServer,
commands: commands,
func getDefaultPrivateKeyPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, ".ssh", "id_rsa"), nil
}

func publicKeyAuthFunc(kPath string) (ssh.AuthMethod, error) {
key, err := os.ReadFile(kPath)
if err != nil {
return nil, fmt.Errorf("unable to read private key: %v", err)
}

signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, fmt.Errorf("unable to parse private key: %v", err)
}

return ssh.PublicKeys(signer), nil
}

func NewTunnelClient(localAddr string, sshServer string, commands string) (*TunnelClient, error) {
privateKeyPath, err := getDefaultPrivateKeyPath()
if err != nil {
return nil, fmt.Errorf("failed to get default private key path: %v", err)
}

log.Infof("get ssh private key file: %v", privateKeyPath)

authMethod, err := publicKeyAuthFunc(privateKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to generate auth method: %v", err)
}

return &TunnelClient{
localAddr: localAddr,
sshServer: sshServer,
commands: commands,
authMethod: authMethod,
}, nil
}

func (c *TunnelClient) Start() error {
config := &ssh.ClientConfig{
User: "v0",
HostKeyCallback: func(string, net.Addr, ssh.PublicKey) error { return nil },
Auth: []ssh.AuthMethod{c.authMethod},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}

conn, err := ssh.Dial("tcp", c.sshServer, config)
@@ -43,17 +85,14 @@ func (c *TunnelClient) Start() error {
return err
}
c.ln = l
ch, req, err := conn.OpenChannel("session", []byte(""))

session, err := c.sshConn.NewSession()
if err != nil {
return err
}
defer ch.Close()
go ssh.DiscardRequests(req)
defer session.Close()

type command struct {
Cmd string
}
_, err = ch.SendRequest("exec", false, ssh.Marshal(command{Cmd: c.commands}))
err = session.Start(c.commands)
if err != nil {
return err
}

0 comments on commit 33684de

Please sign in to comment.