forked from sorintlab/pollon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.go
64 lines (56 loc) · 1.14 KB
/
simple.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
package main
import (
"io/ioutil"
"log"
"net"
"strings"
"time"
"github.com/sorintlab/pollon"
)
func Check(c chan pollon.ConfData) {
conf, err := ioutil.ReadFile("./conf")
if err != nil {
log.Printf("err: %v", err)
c <- pollon.ConfData{DestAddr: nil}
return
}
addrStr := strings.TrimSpace(string(conf))
_, _, err = net.SplitHostPort(addrStr)
if err != nil {
log.Printf("err: %v", err)
c <- pollon.ConfData{DestAddr: nil}
return
}
addr, err := net.ResolveTCPAddr("tcp", addrStr)
if err != nil {
log.Printf("error resolving address: %v", err)
c <- pollon.ConfData{DestAddr: nil}
return
}
log.Printf("address: %s", addr)
c <- pollon.ConfData{DestAddr: addr}
}
func main() {
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:2222")
if err != nil {
log.Fatalf("error: %v", err)
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
log.Fatalf("error: %v", err)
}
proxy, err := pollon.NewProxy(listener)
if err != nil {
log.Fatalf("error: %v", err)
}
go func() {
for {
Check(proxy.C)
time.Sleep(2 * time.Second)
}
}()
err = proxy.Start()
if err != nil {
log.Fatalf("error: %v", err)
}
}