-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdip.go
More file actions
80 lines (67 loc) · 1.33 KB
/
dip.go
File metadata and controls
80 lines (67 loc) · 1.33 KB
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
package main
import (
"flag"
"log"
"math/rand"
"net"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/zztczcx/go-dip/pool"
"github.com/zztczcx/go-dip/tunnel"
)
func init() {
rand.Seed(time.Now().Unix())
}
const SIG_RELOAD = syscall.Signal(35)
const SIG_STATUS = syscall.Signal(36)
func status() {
log.Printf("num goroutines: %d", runtime.NumGoroutine())
buf := make([]byte, 32768)
runtime.Stack(buf, true)
log.Printf("!!!!!stack!!!!!:%s", buf)
}
func monitor(){
c := make(chan os.Signal, 1)
signal.Notify(c, SIG_RELOAD, SIG_STATUS, syscall.SIGTERM, syscall.SIGHUP)
for sig := range c {
switch sig {
case SIG_RELOAD:
//reload()
case SIG_STATUS:
status()
default:
log.Printf("catch siginal: %v, ignored", sig)
}
}
}
func main() {
var laddr string
flag.StringVar(&laddr, "listen", ":1248", "local listen port")
flag.Parse()
go monitor()
go pool.Run()
// run
ln, err := net.Listen("tcp", laddr)
if err != nil {
log.Printf("build listener failed:%s", err.Error())
return
}
defer ln.Close()
// run loop
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("accept failed:%s", err.Error())
if opErr, ok := err.(*net.OpError); ok {
if !opErr.Temporary() {
break
}
}
continue
}
go tunnel.HandleConn(conn.(*net.TCPConn))
}
}