-
Notifications
You must be signed in to change notification settings - Fork 19
/
netcli.go
109 lines (82 loc) · 2.43 KB
/
netcli.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package sshego
import (
"fmt"
"io/ioutil"
"net"
"time"
)
func (cfg *SshegoConfig) TcpClientUserAdd(user *User) (toptPath, qrPath, rsaPath string, err error) {
if cfg.SshegoSystemMutexPort < 0 {
err = fmt.Errorf("SshegoSystemMutexPort was negative(%v),"+
" not possible to add user", cfg.SshegoSystemMutexPort)
return
}
// send newUserCmd followed by the msgp marshalled user
sendMe, err := user.MarshalMsg(nil)
panicOn(err)
addr := fmt.Sprintf("127.0.0.1:%v", cfg.SshegoSystemMutexPort)
nConn, err := net.Dial("tcp", addr)
panicOn(err)
deadline := time.Now().Add(time.Second * 10)
err = nConn.SetDeadline(deadline)
panicOn(err)
_, err = nConn.Write(NewUserCmd)
panicOn(err)
_, err = nConn.Write(sendMe)
panicOn(err)
// read response
deadline = time.Now().Add(time.Second * 10)
err = nConn.SetDeadline(deadline)
panicOn(err)
dat, err := ioutil.ReadAll(nConn)
panicOn(err)
n := len(NewUserReply)
if len(dat) < n {
panic(fmt.Errorf("expected '%s' preamble, but got '%s' of length %v", NewUserReply, string(dat), len(dat)))
}
p("dat = '%v'", string(dat))
payload := dat[n:]
var r User // returned User
_, err = r.UnmarshalMsg(payload)
panicOn(err)
err = nConn.Close()
panicOn(err)
return r.TOTPpath, r.QrPath, r.PrivateKeyPath, nil
}
func (cfg *SshegoConfig) TcpClientUserDel(user *User) error {
if cfg.SshegoSystemMutexPort < 0 {
err := fmt.Errorf("SshegoSystemMutexPort was negative(%v),"+
" not possible to delete user", cfg.SshegoSystemMutexPort)
return err
}
// send newUserCmd followed by the msgp marshalled user
sendMe, err := user.MarshalMsg(nil)
panicOn(err)
addr := fmt.Sprintf("127.0.0.1:%v", cfg.SshegoSystemMutexPort)
nConn, err := net.Dial("tcp", addr)
panicOn(err)
deadline := time.Now().Add(time.Second * 10)
err = nConn.SetDeadline(deadline)
panicOn(err)
_, err = nConn.Write(DelUserCmd)
panicOn(err)
_, err = nConn.Write(sendMe)
panicOn(err)
// read response
deadline = time.Now().Add(time.Second * 10)
err = nConn.SetDeadline(deadline)
panicOn(err)
dat, err := ioutil.ReadAll(nConn)
panicOn(err)
n := len(DelUserReplyFailed)
if len(dat) < n {
panic(fmt.Errorf("expected '%s' preamble, but got '%s' of length %v", NewUserReply, string(dat), len(dat)))
}
p("dat = '%v'", string(dat))
err = nConn.Close()
panicOn(err)
if string(dat[:n]) == string(DelUserReplyFailed) {
return fmt.Errorf("user delete failed -- typically because user does not exist")
}
return nil
}