-
Notifications
You must be signed in to change notification settings - Fork 1
/
dcc.go
140 lines (119 loc) · 2.76 KB
/
dcc.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
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
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
func ntohl(n int64) net.IP {
d := (n & 0xff) << 24
c := (n & 0xff00) << 8
b := (n & 0xff0000) >> 8
a := (n & 0xff000000) >> 24
return net.IPv4(
byte(a&0xff),
byte((b>>8)&0xff),
byte((c>>16)&0xff),
byte((d>>24)&0xff),
)
}
func htonl(ip net.IP) int64 {
a, b, c, d := int64(ip[0]), int64(ip[1]), int64(ip[2]), int64(ip[3])
return a<<24 | b<<16 | c<<8 | d
}
func wanIP() (net.IP, error) {
resp, err := http.Get("http://icanhazip.com")
if err != nil {
return nil, err
}
buf := make([]byte, 16)
resp.Body.Read(buf)
i := 15
for ; buf[i] != '\n'; i-- {
}
return net.ParseIP(string(buf[:i])), nil
}
func localIP() (net.IP, error) {
ifaceAddrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, ifaceAddr := range ifaceAddrs {
ifaceIP := net.ParseIP(strings.Split(ifaceAddr.String(), "/")[0])
if ifaceIP != nil {
if ifaceIP.To4() != nil {
return ifaceIP.To4(), nil
}
}
}
return nil, fmt.Errorf("couldn't bind to an ip4 address on the local machine")
}
func fileTransfer(servConn *serverConnection, who, filename string) {
f, err := os.Open(filename)
checkErr(err)
stat, err := f.Stat()
checkErr(err)
filesize := stat.Size()
if servConn.ip == nil {
servConn.ip, _ = localIP()
}
ip := servConn.ip
ln, err := net.Listen("tcp", ip.String()+":0")
checkErr(err)
go func() {
for {
conn, err := ln.Accept()
checkErr(err)
io.Copy(conn, f)
f.Close()
conn.Close()
return
}
}()
addr := strings.Split(ln.Addr().String(), ":")
log.Println("listening on", addr)
ipnl := htonl(ip)
port, _ := strconv.ParseInt(addr[1], 10, 64)
ctcpMsg := fmt.Sprintf("DCC SEND %s %v %v %v", filename, ipnl, port, filesize)
log.Println(ctcpMsg)
servConn.conn.Ctcp(who, ctcpMsg)
}
func dccHandler(servConn *serverConnection, who, req string) {
args := strings.Split(req, " ")
switch args[0] {
case "SEND":
filename := args[1]
ipnl, _ := strconv.ParseInt(args[2], 10, 64)
ip := ntohl(ipnl)
port, _ := strconv.Atoi(args[3])
filesize, _ := strconv.Atoi(args[4])
log.Println("got DCC SEND from", who, ":", filename, ip, port, filesize)
servConn.conn.Ctcp(who, "DCC ACCEPT", filename, fmt.Sprintf("%d", port), "0")
f, err := os.Create(filename)
checkErr(err)
defer f.Close()
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", ip, port))
checkErr(err)
size := 0
for {
b := make([]byte, 32)
n, err := conn.Read(b)
size += n
if err == io.EOF || size >= filesize {
conn.Close()
break
} else if err != nil {
log.Println(err)
break
}
}
log.Println(filesize, size, err)
log.Println(filename, ": file transfer complete")
default:
log.Println("got DCC request but did not process:", req)
}
}