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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
package main
import (
"bufio"
"crypto/tls"
"errors"
"io"
"log"
"net"
"os"
"sync"
ircgomsg "github.com/ergochat/irc-go/ircmsg"
"golang.org/x/net/proxy"
)
func RunGhost(ghostNetwork GhostNetwork, name string) {
var listener net.Listener
var err error
if ghostNetwork.ServerKey == "" || ghostNetwork.ServerCert == "" {
log.Printf("Ghost %s: either one or both of ServerKey and ServerCert were not provided. ghosty will not be listening on TLS.", name)
listener, err = net.Listen("tcp", ghostNetwork.ListenAddress)
if err != nil {
log.Fatalf("Ghost %s: Failed to listen on %s: %v", name, ghostNetwork.ListenAddress, err)
}
} else {
tlsCert, err := os.ReadFile(ghostNetwork.ServerCert)
if err != nil {
log.Fatalf("Ghost %s: Failed to read TLS certificate file: %v", name, err)
}
tlsKey, err := os.ReadFile(ghostNetwork.ServerKey)
if err != nil {
log.Fatalf("Ghost %s: Failed to read TLS key file: %v", name, err)
}
cert, err := tls.X509KeyPair(tlsCert, tlsKey)
if err != nil {
log.Fatalf("Ghost %s: Failed to load TLS key pair: %v", name, err)
}
listener, err = tls.Listen("tcp", ghostNetwork.ListenAddress, &tls.Config{
MinVersion: tls.VersionTLS13,
Certificates: []tls.Certificate{cert},
})
if err != nil {
log.Fatalf("Ghost %s: Failed to listen on %s: %v", name, ghostNetwork.ListenAddress, err)
}
}
defer listener.Close()
log.Printf("Ghost %s: IRC Bouncer listening on %s", name, ghostNetwork.ListenAddress)
log.Printf("Ghost %s: Connecting clients to IRC server: %s", name, ghostNetwork.ServerAddress)
for {
clientConn, err := listener.Accept()
if err != nil {
log.Printf("Ghost %s: Failed to accept client connection: %v", name, err)
continue
}
go handleClientConnection(clientConn, ghostNetwork, name)
}
}
func proxyConnectinoTLS(clientConn net.Conn, ghostNetwork GhostNetwork, name string) *tls.Conn {
dialer, err := proxy.SOCKS5("tcp", ghostNetwork.UpstreamProxy, nil, proxy.Direct)
if err != nil {
log.Fatalf("Ghost %s: Failed to create SOCKS5 dialer: %v", name, err)
}
proxyCon, err := dialer.Dial("tcp", ghostNetwork.ServerAddress)
if err != nil {
log.Printf("Ghost %s: Failed to connect to IRC server %s via proxy %s: %v", name, ghostNetwork.ServerAddress, ghostNetwork.UpstreamProxy, err)
clientConn.Close()
return nil
}
var tlsConfig *tls.Config
if ghostNetwork.CertPath != "" && ghostNetwork.KeyPath != "" {
clientCert, err := tls.LoadX509KeyPair(ghostNetwork.CertPath, ghostNetwork.KeyPath)
if err != nil {
log.Fatalf("Ghost %s: Failed to load client TLS key pair: %v", name, err)
}
tlsConfig = &tls.Config{
ServerName: ghostNetwork.ServerName,
Certificates: []tls.Certificate{clientCert},
InsecureSkipVerify: ghostNetwork.SkipTLSVerify,
MinVersion: tls.VersionTLS13,
}
} else {
tlsConfig = &tls.Config{
ServerName: ghostNetwork.ServerName,
InsecureSkipVerify: ghostNetwork.SkipTLSVerify,
MinVersion: tls.VersionTLS13,
}
}
conn := tls.Client(proxyCon, tlsConfig)
err = conn.Handshake()
if err != nil {
log.Printf("Ghost %s: TLS handshake with IRC server %s failed: %v", name, ghostNetwork.ServerAddress, err)
clientConn.Close()
return nil
}
return conn
}
func connectionTLS(clientConn net.Conn, ghostNetwork GhostNetwork, name string) *tls.Conn {
clientCert, err := tls.LoadX509KeyPair(ghostNetwork.CertPath, ghostNetwork.KeyPath)
if err != nil {
log.Fatalf("Ghost %s: Failed to load client TLS key pair: %v", name, err)
}
tlsConfig := &tls.Config{
ServerName: ghostNetwork.ServerName,
Certificates: []tls.Certificate{clientCert},
InsecureSkipVerify: ghostNetwork.SkipTLSVerify,
MinVersion: tls.VersionTLS13,
}
conn, err := tls.Dial("tcp", ghostNetwork.ServerAddress, tlsConfig)
if err != nil {
log.Printf("Ghost %s: Failed to connect to IRC server %s with TLS: %v", name, ghostNetwork.ServerAddress, err)
clientConn.Close()
return nil
}
return conn
}
func proxyConnection(clientConn net.Conn, ghostNetwork GhostNetwork, name string) net.Conn {
dialer, err := proxy.SOCKS5("tcp", ghostNetwork.UpstreamProxy, nil, proxy.Direct)
if err != nil {
log.Fatalf("Ghost %s: Failed to create SOCKS5 dialer: %v", name, err)
}
proxyCon, err := dialer.Dial("tcp", ghostNetwork.ServerAddress)
if err != nil {
log.Printf("Ghost %s: Failed to connect to IRC server %s via proxy %s: %v", name, ghostNetwork.ServerAddress, ghostNetwork.UpstreamProxy, err)
clientConn.Close()
return nil
}
return proxyCon
}
func handleClientConnection(clientConn net.Conn, ghostNetwork GhostNetwork, name string) {
log.Printf("Ghost %s: Client connected from: %s", name, clientConn.RemoteAddr())
var ircServerConn net.Conn
var err error
if ghostNetwork.UpstreamProxy == "" {
if ghostNetwork.UseTLS {
ircServerConn = connectionTLS(clientConn, ghostNetwork, name)
} else {
ircServerConn, err = net.Dial("tcp", ghostNetwork.ServerAddress)
if err != nil {
log.Printf("Ghost %s: Failed to connect to IRC server %s: %v", name, ghostNetwork.ServerAddress, err)
clientConn.Close()
return
}
}
} else {
if ghostNetwork.UseTLS {
ircServerConn = proxyConnectinoTLS(clientConn, ghostNetwork, name)
} else {
ircServerConn = proxyConnection(clientConn, ghostNetwork, name)
}
}
if ircServerConn == nil {
return
}
log.Printf("Ghost %s: Successfully connected to IRC server: %s", name, ghostNetwork.ServerAddress)
done := make(chan struct{})
var once sync.Once
closeConnections := func() {
once.Do(func() {
clientConn.Close()
ircServerConn.Close()
close(done)
})
}
go relay(ghostNetwork.LogRaw, clientConn, ircServerConn, closeConnections, name)
go relay(ghostNetwork.LogRaw, ircServerConn, clientConn, closeConnections, name)
<-done
}
func relay(logRaw bool, src, dest net.Conn, closer func(), name string) {
reader := bufio.NewReader(src)
for {
line, err := reader.ReadString('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
log.Printf("Ghost %s: Relay read error on: %v", name, err)
}
closer()
return
}
msg, err := ircgomsg.ParseLine(line)
if err != nil {
log.Printf("Ghost %s: Failed to parse IRC message: %v", name, err)
}
if msg.Command == "PRIVMSG" {
}
if msg.Command == "MSG" {
}
if logRaw {
log.Printf("Ghost %s: %v", name, msg)
}
_, err = io.WriteString(dest, line)
if err != nil {
closer()
return
}
}
}
|