-
Notifications
You must be signed in to change notification settings - Fork 42
/
recon_unix.go
79 lines (66 loc) · 1.42 KB
/
recon_unix.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
// +build freebsd openbsd dragonfly netbsd darwin
package hbot
import (
"fmt"
"net"
"syscall"
"github.com/ftrvxmtrx/fd"
)
// StartUnixListener starts up a unix domain socket listener for reconnects to
// be sent through
func (bot *Bot) StartUnixListener() {
unaddr, err := net.ResolveUnixAddr("unix", bot.unixsock)
if err != nil {
panic(err)
}
// Unlink the socket so we don't have to worry about removing it
// We can ignore any error here
syscall.Unlink(bot.unixsock)
list, err := net.ListenUnix("unix", unaddr)
if err != nil {
panic(err)
}
defer list.Close()
bot.unixlist = list
con, err := list.AcceptUnix()
if err != nil {
fmt.Println("unix listener error: ", err)
return
}
defer con.Close()
fi, err := bot.con.(*net.TCPConn).File()
if err != nil {
panic(err)
}
err = fd.Put(con, fi)
if err != nil {
panic(err)
}
select {
case <-bot.Incoming:
default:
close(bot.Incoming)
}
close(bot.outgoing)
}
// Attempt to hijack session previously running bot
func (bot *Bot) hijackSession() bool {
con, err := net.Dial("unix", bot.unixsock)
if err != nil {
bot.Info("Couldnt restablish connection, no prior bot.", "err", err)
return false
}
defer con.Close()
ncon, err := fd.Get(con.(*net.UnixConn), 1, nil)
if err != nil {
panic(err)
}
defer ncon[0].Close()
netcon, err := net.FileConn(ncon[0])
if err != nil {
panic(err)
}
bot.reconnecting = true
bot.con = netcon
return true
}