-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpeerproxy.go
71 lines (60 loc) · 1.4 KB
/
peerproxy.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
package main
import (
"fmt"
"libpeerproxy"
"log"
"os"
"time"
//"strconv"
)
// const UPDATETIME = 3600 // default per hour an update
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run peerproxy.go [peer host]")
return
}
p := libpeerproxy.NewProxyServer()
host, err := libpeerproxy.ExternalIP()
if err != nil {
fmt.Println("ExternalIP :", err)
}
fmt.Println("My own IP Address: " + host)
host = os.Args[1]
port := "7890"
proxyPort := "3128"
addr := host + ":" + port
proxyAddr := host + ":" + proxyPort
contact := libpeerproxy.Contact{host, port, proxyPort, addr, proxyAddr, -1}
go p.DoPing(contact)
// periodically update ContactList
updateCh := make(chan bool)
// var updateTime time.Duration
// if len(os.Args) > 2 {
// updateTime, _ = strconv.Atoi(os.Args[2])
// } else {
// updateTime = UPDATETIME
// }
go func() {
for {
time.Sleep(60 * time.Second) // customized for testing purpose
updateCh <- true
}
}()
go func() {
for {
update := <-updateCh
if update == true {
p.ContactList.PrintContactList()
err := p.DoUpdateContactList()
if err != nil {
log.Println("Error DoUpdateContactList: ", err.Error())
}
}
}
}()
// Every ProxyServer serves as a proxy at addr proxyServerAddr
go p.ServeAsProxy()
// Every ProxyServer peer also serve as a proxyRouter,
// only for routing requests of itself
p.ServerAsProxyRouter()
}