-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
76 lines (65 loc) · 1.95 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"strings"
)
var Debug bool
//just wrapper
func ConcatStr (sep string, args ... string) string {
return strings.Join(args, sep)
}
func main() {
//arguments
Control_socket := flag.String("control", "127.0.0.1:7722", "control UDP socket")
External_ip := flag.String("external", "", "external address")
Internal_ip := flag.String("internal", "", "internal address")
Debug_arg := flag.Bool("debug", false, "debug output")
flag.Parse()
//not working without external IP
if ( *External_ip == "" ) {
flag.PrintDefaults()
return
}
//without Internal IP use External
if ( *Internal_ip == "" ) {
*Internal_ip = *External_ip
}
Debug = *Debug_arg
//start listening control socket
ServerAddr, err := net.ResolveUDPAddr("udp", *Control_socket)
if err != nil {
fmt.Println("Control socket: ", err)
return
}
ServerConn, err := net.ListenUDP("udp", ServerAddr)
if err != nil {
fmt.Println("Control socket: ", err)
return
}
defer ServerConn.Close()
//chan for commands from kamailio with buffer 100 commands
Command_chan := make(chan ProxyCommand, 100)
//chan for response to kamailio with buffer 100 commands
Response_chan := make(chan ProxyCommand, 100)
//start comand handler
go ReqHandler (Command_chan, Response_chan, *External_ip, *Internal_ip)
go RespSender (Response_chan, ServerConn)
//read commands from kamailio with buffer 1024 bytes
Buffer := make([]byte, 1024)
for {
Nbytes, Raddr, err := ServerConn.ReadFromUDP(Buffer)
if err != nil {
fmt.Println("Read from control socket: ", err)
break
}
var Recv ProxyCommand
Recv.command = string(Buffer[0:Nbytes])
Recv.raddr = Raddr
Command_chan <- Recv
if Debug {
fmt.Println("Received: ",Recv.command, " from ",Recv.raddr)
}
}
}