-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipc.go
172 lines (141 loc) · 3.68 KB
/
ipc.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"net/rpc"
"os"
"runtime"
"github.com/shirou/gopsutil/cpu"
"gopkg.in/natefinch/npipe.v2"
)
type Args struct {
A, B int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
type StartArgs struct {
Name string
Path string
Command string
}
type StartReply struct {
Success bool
Message string
}
type Reply struct {
Success bool
Message string
}
type Server struct{}
func (t *Server) Start(args *StartArgs, reply *Reply) error {
// create a channel to wait for the process to start
c := make(chan bool, 1)
// start the process
go startProject(Service{Name: args.Name, Path: args.Path, Command: args.Command}, &globalEnv, reply, c)
<-c
// return success
return nil
}
func (t *Server) Stop(args *StartArgs, reply *Reply) error {
// stop the process
stopProject(args.Name, reply)
// return success
return nil
}
func (t *Server) Dump(args *StartArgs, reply *Reply) error {
// dump memory of the process
dumpProject(args.Name, reply)
// return success
return nil
}
func (t *Server) Restart(args *StartArgs, reply *Reply) error {
// restart the process
c := make(chan bool, 1)
restartProject(args.Name, reply, c)
<-c // wait for the process to restart before returning
return nil
}
func GeneratePipeName() string {
// Get cpu info and mac address
cpuInfo, _ := cpu.Info()
macAddress, _ := net.Interfaces()
// Generate a hash from the cpu info and mac address
hash := sha256.New()
hash.Write([]byte(cpuInfo[0].ModelName + macAddress[0].HardwareAddr.String()))
// Return the hash as a string
if runtime.GOOS == "windows" {
return fmt.Sprintf(`\\.\pipe\%s.gpm.rpc`, hex.EncodeToString(hash.Sum(nil)))
} else {
return fmt.Sprintf(`/tmp/%s.gpm.rpc`, hex.EncodeToString(hash.Sum(nil)))
}
}
var pipeName = GeneratePipeName()
func StartIpcServer() {
var listener net.Listener
var err error
// Determine the operating system
if runtime.GOOS == "windows" {
// Use npipe package to create named pipe on Windows
listener, err = npipe.Listen(pipeName)
if err != nil {
fmt.Println("Error creating named pipe:", err)
os.Exit(1)
}
fmt.Println("Listening on named pipe:", GeneratePipeName())
// send a message to the client
} else {
// Use net package to create Unix domain socket on Unix
os.Remove(GeneratePipeName())
listener, err = net.Listen("unix", GeneratePipeName())
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
fmt.Println("Listening on Unix domain socket:", GeneratePipeName())
}
defer listener.Close()
// Register the RPC server
rpc.Register(new(Server))
// Accept incoming connections
for {
conn, err := listener.Accept()
// send a message to the client
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
// read the message
go rpc.ServeConn(conn)
}
}
func ConnectToIpcServer() *rpc.Client {
var client *rpc.Client
var err error
// Determine the operating system
if runtime.GOOS == "windows" {
// Use npipe package to connect to named pipe on Windows
c, err := npipe.Dial(GeneratePipeName())
// send a message to the client
if err != nil {
fmt.Println("Error connecting to named pipe:", err)
os.Exit(1)
}
client = rpc.NewClient(c)
fmt.Println("Connected to named pipe:", GeneratePipeName())
} else {
// Use net package to connect to Unix domain socket on Unix
client, err = rpc.Dial("unix", GeneratePipeName())
if err != nil {
fmt.Println("Error connecting to Unix domain socket:", err)
os.Exit(1)
}
fmt.Println("Connected to Unix domain socket:", GeneratePipeName())
}
// Call the RPC method
return client
}