This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsetup.go
110 lines (92 loc) · 2.3 KB
/
setup.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
110
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"text/template"
"gitlab.com/aki237/pin/pinlib"
)
func executeTemplate(script string, env interface{}) (string, error) {
sb := bytes.NewBuffer(nil)
t, err := template.New("script").Parse(script)
if err != nil {
return "", err
}
err = t.Execute(sb, env)
return sb.String(), err
}
func runScript(script string) error {
fmt.Printf("Executing:\n\t%s\n", strings.ReplaceAll(script, "\n", "\n\t"))
cmd := exec.Command("sh", "-c", script)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
return cmd.Run()
}
func (s *Session) SetupClient() {
client, ok := s.peer.(*pinlib.Client)
if !ok {
return
}
client.Hook = func(ipp, gw string) error {
s.InterfaceAddress = ipp
s.InterfaceGateway = gw
scriptTmpl, ok := s.Config.PostConnectScript[runtime.GOOS]
if !ok {
fmt.Printf("[WARN] No post connect script defined for '%s' platform", runtime.GOOS)
return nil
}
script, err := executeTemplate(scriptTmpl, map[string]interface{}{
"interfaceName": s.InterfaceName,
"mtu": s.MTU,
"remoteIP": s.ResolvedRemoteIP.String(),
"remotePort": s.RemotePort,
"tunIP": ipp,
"tunGateway": gw,
"dns": s.DNS,
})
if err != nil {
return err
}
return runScript(script)
}
}
func (s *Session) SetupServer() error {
scriptTmpl, ok := s.Config.PostInitScript[runtime.GOOS]
if !ok {
fmt.Printf("[WARN] No post init script defined for '%s' platform", runtime.GOOS)
return nil
}
script, err := executeTemplate(scriptTmpl, map[string]interface{}{
"interfaceName": s.InterfaceName,
"mtu": s.MTU,
"tunIP": s.DHCP,
"dns": s.DNS,
})
if err != nil {
return err
}
return runScript(script)
}
func (s *Session) StopClient() error {
scriptTmpl, ok := s.Config.PostDisconnectScript[runtime.GOOS]
if !ok {
fmt.Printf("[WARN] No post init script defined for '%s' platform", runtime.GOOS)
return nil
}
script, err := executeTemplate(scriptTmpl, map[string]interface{}{
"interfaceName": s.InterfaceName,
"remoteIP": s.ResolvedRemoteIP.String(),
"remotePort": s.RemotePort,
"mtu": s.MTU,
"tunIP": s.DHCP,
"dns": s.DNS,
})
if err != nil {
return err
}
return runScript(script)
}