commit 4969650
Hubert Hirtz
·
2021-05-17 20:40:09 +0000 UTC
parent 36f278f
Add port if missing (v2) and don't set keepalives
1 files changed,
+9,
-22
M
app.go
M
app.go
+9,
-22
1@@ -168,38 +168,25 @@ func (app *App) connect() {
2
3 func (app *App) tryConnect() (err error) {
4 addr := app.cfg.Addr
5- host, port, err := net.SplitHostPort(addr)
6- if err != nil {
7- return err
8- }
9- if port == "" {
10+ colonIdx := strings.LastIndexByte(addr, ':')
11+ bracketIdx := strings.LastIndexByte(addr, ']')
12+ if colonIdx <= bracketIdx {
13+ // either colonIdx < 0, or the last colon is before a ']' (end
14+ // of IPv6 address. -> missing port
15 if app.cfg.NoTLS {
16- port = "6667"
17+ addr += ":6667"
18 } else {
19- port = "6697"
20+ addr += ":6697"
21 }
22 }
23
24- peerAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, port))
25- if err != nil {
26- return
27- }
28-
29- tcpConn, err := net.DialTCP("tcp", nil, peerAddr)
30+ conn, err := net.Dial("tcp", addr)
31 if err != nil {
32 return
33 }
34- if err = tcpConn.SetKeepAlivePeriod(1 * time.Minute); err != nil {
35- tcpConn.Close()
36- return
37- }
38- if err = tcpConn.SetKeepAlive(true); err != nil {
39- tcpConn.Close()
40- return
41- }
42
43- var conn net.Conn = tcpConn
44 if !app.cfg.NoTLS {
45+ host, _, _ := net.SplitHostPort(addr) // should succeed since net.Dial did.
46 conn = tls.Client(conn, &tls.Config{
47 ServerName: host,
48 NextProtos: []string{"irc"},