Browse Source

TLS support

tags/v0.1.0
Jeremy Latt 10 years ago
parent
commit
db0f494604
4 changed files with 38 additions and 9 deletions
  1. 1
    3
      .gitignore
  2. 11
    1
      irc/config.go
  3. 1
    1
      irc/constants.go
  4. 25
    4
      irc/server.go

+ 1
- 3
.gitignore View File

@@ -1,4 +1,2 @@
1
-pkg
2
-bin
3
-ergonomadic.db
4 1
 ergonomadic.json
2
+*.pem

+ 11
- 1
irc/config.go View File

@@ -7,7 +7,7 @@ import (
7 7
 
8 8
 type Config struct {
9 9
 	Name      string
10
-	Listen    string
10
+	Listeners []ListenerConfig
11 11
 	Password  string
12 12
 	Operators []OperatorConfig
13 13
 	Debug     map[string]bool
@@ -18,6 +18,16 @@ type OperatorConfig struct {
18 18
 	Password string
19 19
 }
20 20
 
21
+type ListenerConfig struct {
22
+	Address     string
23
+	Key         string
24
+	Certificate string
25
+}
26
+
27
+func (config *ListenerConfig) IsTLS() bool {
28
+	return (config.Key != "") && (config.Certificate != "")
29
+}
30
+
21 31
 func LoadConfig() (config *Config, err error) {
22 32
 	config = &Config{}
23 33
 

+ 1
- 1
irc/constants.go View File

@@ -15,7 +15,7 @@ var (
15 15
 )
16 16
 
17 17
 const (
18
-	VERSION       = "ergonomadic-1"
18
+	VERSION       = "1.0.0"
19 19
 	CRLF          = "\r\n"
20 20
 	MAX_REPLY_LEN = 512 - len(CRLF)
21 21
 

+ 25
- 4
irc/server.go View File

@@ -2,6 +2,7 @@ package irc
2 2
 
3 3
 import (
4 4
 	"crypto/rand"
5
+	"crypto/tls"
5 6
 	"encoding/binary"
6 7
 	"fmt"
7 8
 	"log"
@@ -37,7 +38,11 @@ func NewServer(config *Config) *Server {
37 38
 	}
38 39
 
39 40
 	go server.receiveCommands(commands)
40
-	go server.listen(config.Listen)
41
+
42
+	for _, listenerConf := range config.Listeners {
43
+		go server.listen(listenerConf)
44
+	}
45
+
41 46
 	return server
42 47
 }
43 48
 
@@ -63,14 +68,30 @@ func (server *Server) receiveCommands(commands <-chan Command) {
63 68
 	}
64 69
 }
65 70
 
66
-func (s *Server) listen(addr string) {
67
-	listener, err := net.Listen("tcp", addr)
71
+func newListener(config ListenerConfig) (net.Listener, error) {
72
+	if config.IsTLS() {
73
+		certificate, err := tls.LoadX509KeyPair(config.Certificate, config.Key)
74
+		if err != nil {
75
+			return nil, err
76
+		}
77
+		return tls.Listen("tcp", config.Address, &tls.Config{
78
+			Certificates:             []tls.Certificate{certificate},
79
+			PreferServerCipherSuites: true,
80
+			MinVersion:               tls.VersionTLS12,
81
+		})
82
+	}
83
+
84
+	return net.Listen("tcp", config.Address)
85
+}
86
+
87
+func (s *Server) listen(config ListenerConfig) {
88
+	listener, err := newListener(config)
68 89
 	if err != nil {
69 90
 		log.Fatal("Server.Listen: ", err)
70 91
 	}
71 92
 
72 93
 	s.hostname = LookupHostname(listener.Addr())
73
-	log.Print("Server.Listen: listening on ", addr)
94
+	log.Print("Server.Listen: listening on ", config.Address)
74 95
 
75 96
 	for {
76 97
 		conn, err := listener.Accept()

Loading…
Cancel
Save