-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
74 lines (61 loc) · 1.64 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
a simple test client. Change the settings at the top of main()
to manually test your setup.
*/
package main
import (
"fmt"
"os"
"github.com/glycerine/sshego"
)
func main() {
// you'll have to set these to do
// manual testing. These are just guesses/to get you started.
home := os.Getenv("HOME")
user := os.Getenv("USER")
sshd := "example.com" // definitely must be changed.
target := "127.0.0.1:8888"
// N.B. we will append any newly seen hosts to this file
// if addNewHost is true.
kh := home + "/.ssh/known_hosts"
rsaPath := home + "/.ssh/id_rsa" // definitely must be set, unlikely to be correct as is.
addNewHost := false
// done with settings
dc := sshego.DialConfig{
ClientKnownHostsPath: kh,
Mylogin: user,
RsaPath: rsaPath,
Sshdhost: sshd,
Sshdport: 22,
DownstreamHostPort: target,
TofuAddIfNotKnown: addNewHost,
}
channelToTcpServer, _, err := dc.Dial()
panicOn(err)
confirmationPayload := "ping"
m, err := channelToTcpServer.Write([]byte(confirmationPayload))
panicOn(err)
if m != len(confirmationPayload) {
panic("too short a write!")
}
payloadByteCount := 4
confirmationReply := "pong"
// check reply
rep := make([]byte, payloadByteCount)
m, err = channelToTcpServer.Read(rep)
panicOn(err)
if m != payloadByteCount {
panic("too short a reply!")
}
srep := string(rep)
if srep != confirmationReply {
panic(fmt.Errorf("saw '%s' but expected '%s'", srep, confirmationReply))
}
fmt.Printf("reply success! we got the expected srep reply '%s'\n", srep)
channelToTcpServer.Close()
}
func panicOn(err error) {
if err != nil {
panic(err)
}
}