Skip to content

Commit af54c11

Browse files
committed
Proxy with certificates
1 parent 4b9cdd9 commit af54c11

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

pkg/proxy/proxy.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type KiwiProxyConnection struct {
3131
DestTLS bool
3232
State KiwiProxyState
3333
Conn *net.Conn
34+
WebircPemCert []byte
35+
WebircPemKey []byte
3436
}
3537

3638
func MakeKiwiProxyConnection() *KiwiProxyConnection {
@@ -63,11 +65,13 @@ func (c *KiwiProxyConnection) Dial(proxyServerAddr string) error {
6365
c.State = KiwiProxyStateHandshaking
6466

6567
meta, _ := json.Marshal(map[string]interface{}{
66-
"username": c.Username,
67-
"interface": c.ProxyInterface,
68-
"host": c.DestHost,
69-
"port": c.DestPort,
70-
"ssl": c.DestTLS,
68+
"username": c.Username,
69+
"interface": c.ProxyInterface,
70+
"host": c.DestHost,
71+
"port": c.DestPort,
72+
"ssl": c.DestTLS,
73+
"webirc_cert": c.WebircPemCert,
74+
"webirc_key": c.WebircPemKey,
7175
})
7276

7377
(*c.Conn).Write(append(meta, byte('\n')))

pkg/proxy/server.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,19 @@ var identdRpc *identd.RpcClient
2929
var Server net.Listener
3030

3131
type HandshakeMeta struct {
32-
Host string `json:"host"`
33-
Port int `json:"port"`
34-
TLS bool `json:"ssl"`
35-
Username string `json:"username"`
36-
Interface string `json:"interface"`
32+
Host string `json:"host"`
33+
Port int `json:"port"`
34+
TLS bool `json:"ssl"`
35+
Username string `json:"username"`
36+
Interface string `json:"interface"`
37+
WebircPemCert []byte `json:"webirc_cert"`
38+
WebircPemKey []byte `json:"webirc_key"`
3739
}
3840

39-
func MakeClient(conn net.Conn, webircCert *tls.Certificate) *Client {
40-
client := &Client{
41+
func MakeClient(conn net.Conn) *Client {
42+
return &Client{
4143
Client: conn,
4244
}
43-
if webircCert != nil {
44-
client.WebircCertificate = []tls.Certificate{*webircCert}
45-
}
46-
return client
4745
}
4846

4947
type Client struct {
@@ -93,6 +91,13 @@ func (c *Client) Handshake() error {
9391
return unmarshalErr
9492
}
9593

94+
if len(meta.WebircPemCert) > 0 && len(meta.WebircPemKey) > 0 {
95+
webircCert, err := tls.X509KeyPair(meta.WebircPemCert, meta.WebircPemKey)
96+
if err == nil {
97+
c.WebircCertificate = []tls.Certificate{webircCert}
98+
}
99+
}
100+
96101
if meta.Host == "" || meta.Port == 0 || meta.Username == "" || meta.Interface == "" {
97102
c.Client.Write([]byte(ResponseError))
98103
return fmt.Errorf("missing args")
@@ -148,7 +153,10 @@ func (c *Client) ConnectUpstream() error {
148153
}
149154

150155
if c.TLS {
151-
tlsConfig := &tls.Config{InsecureSkipVerify: true}
156+
tlsConfig := &tls.Config{
157+
InsecureSkipVerify: true,
158+
Certificates: c.WebircCertificate,
159+
}
152160
tlsConn := tls.Client(conn, tlsConfig)
153161
err := tlsConn.Handshake()
154162
if err != nil {
@@ -190,7 +198,7 @@ func (c *Client) Pipe() {
190198
}
191199
}
192200

193-
func Start(laddr string, webircCert *tls.Certificate) {
201+
func Start(laddr string) {
194202
srv, err := net.Listen("tcp", laddr)
195203
if err != nil {
196204
log.Fatal(err.Error())
@@ -210,7 +218,7 @@ func Start(laddr string, webircCert *tls.Certificate) {
210218
break
211219
}
212220

213-
c := MakeClient(conn, webircCert)
221+
c := MakeClient(conn)
214222
go c.Run()
215223
}
216224
}

pkg/webircgateway/client.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ func (c *Client) makeUpstreamConnection() (io.ReadWriteCloser, error) {
303303
client := c
304304
upstreamConfig := c.UpstreamConfig
305305

306+
// TODO remove me
307+
upstreamConfig.Proxy = &ConfigProxy{
308+
Type: "kiwi",
309+
Hostname: "127.0.0.1",
310+
Port: 7999,
311+
TLS: false,
312+
Username: client.IrcState.Username,
313+
Interface: "0.0.0.0",
314+
}
306315
var connection io.ReadWriteCloser
307316

308317
if upstreamConfig.Proxy == nil {
@@ -370,6 +379,8 @@ func (c *Client) makeUpstreamConnection() (io.ReadWriteCloser, error) {
370379
conn.DestTLS = upstreamConfig.TLS
371380
conn.Username = upstreamConfig.Proxy.Username
372381
conn.ProxyInterface = upstreamConfig.Proxy.Interface
382+
conn.WebircPemCert = upstreamConfig.WebircPemCert
383+
conn.WebircPemKey = upstreamConfig.WebircPemKey
373384

374385
dialErr := conn.Dial(fmt.Sprintf(
375386
"%s:%d",
@@ -703,10 +714,12 @@ func (c *Client) configureUpstream() ConfigUpstream {
703714
upstreamConfig.Timeout = c.Gateway.Config.GatewayTimeout
704715
upstreamConfig.Throttle = c.Gateway.Config.GatewayThrottle
705716
upstreamConfig.WebircPassword = c.Gateway.findWebircPassword(c.DestHost)
717+
upstreamConfig.WebircPemCert = c.Gateway.Config.WebircPemCert
718+
upstreamConfig.WebircPemKey = c.Gateway.Config.WebircPemKey
706719

707-
if c.Gateway.Config.WebircCert != nil {
720+
if c.Gateway.Config.WebircCertificate.Certificate != nil {
708721
upstreamConfig.WebircCertificate = []tls.Certificate{
709-
*c.Gateway.Config.WebircCert,
722+
*c.Gateway.Config.WebircCertificate,
710723
}
711724
}
712725
return upstreamConfig

pkg/webircgateway/config.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package webircgateway
33
import (
44
"crypto/tls"
55
"errors"
6+
"io/ioutil"
67
"net"
78
"os"
89
"os/exec"
@@ -29,6 +30,8 @@ type ConfigUpstream struct {
2930
GatewayName string
3031
Proxy *ConfigProxy
3132
WebircCertificate []tls.Certificate
33+
WebircPemCert []byte
34+
WebircPemKey []byte
3235
}
3336

3437
// ConfigServer - A web server config
@@ -79,7 +82,9 @@ type Config struct {
7982
ReCaptchaSecret string
8083
ReCaptchaKey string
8184
Secret string
82-
WebircCert *tls.Certificate
85+
WebircCertificate *tls.Certificate
86+
WebircPemCert []byte
87+
WebircPemKey []byte
8388
Plugins []string
8489
DnsblServers []string
8590
// DnsblAction - "deny" = deny the connection. "verify" = require verification
@@ -151,7 +156,9 @@ func (c *Config) Load() error {
151156
c.ReCaptchaKey = ""
152157
c.RequiresVerification = false
153158
c.Secret = ""
154-
c.WebircCert = nil
159+
c.WebircCertificate = nil
160+
c.WebircPemCert = make([]byte, 0)
161+
c.WebircPemKey = make([]byte, 0)
155162
c.SendQuitOnClientClose = ""
156163
c.ClientRealname = ""
157164
c.ClientUsername = ""
@@ -183,9 +190,22 @@ func (c *Config) Load() error {
183190
if webircCert != "" && webircKey != "" {
184191
certPath := c.ResolvePath(webircCert)
185192
keyPath := c.ResolvePath(webircKey)
186-
webircCert, err := tls.LoadX509KeyPair(certPath, keyPath)
193+
194+
c.WebircPemCert, err = ioutil.ReadFile(certPath)
195+
if err != nil {
196+
c.gateway.Log(3, "Failed to load webirc certificate, "+err.Error())
197+
continue
198+
}
199+
200+
c.WebircPemKey, err = ioutil.ReadFile(keyPath)
201+
if err != nil {
202+
c.gateway.Log(3, "Failed to load webirc certificate, "+err.Error())
203+
continue
204+
}
205+
206+
webircCert, err := tls.X509KeyPair(c.WebircPemCert, c.WebircPemKey)
187207
if err == nil {
188-
c.WebircCert = &webircCert
208+
c.WebircCertificate = &webircCert
189209
} else {
190210
c.gateway.Log(3, "Failed to load webirc certificate, "+err.Error())
191211
}

pkg/webircgateway/gateway.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ func (s *Gateway) Start() {
7878
}
7979

8080
if s.Function == "proxy" {
81-
proxy.Start(
82-
fmt.Sprintf("%s:%d", s.Config.Proxy.LocalAddr, s.Config.Proxy.Port),
83-
s.Config.WebircCert,
84-
)
81+
proxy.Start(fmt.Sprintf("%s:%d", s.Config.Proxy.LocalAddr, s.Config.Proxy.Port))
8582
}
8683
}
8784

0 commit comments

Comments
 (0)