1
|
|
|
package goagi |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bufio" |
5
|
|
|
"net" |
6
|
|
|
"os" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
/* |
10
|
|
|
NewAGI creates and returns AGI object. |
11
|
|
|
Parses AGI arguments and set ready for communication. |
12
|
|
|
|
13
|
|
|
Usage example: |
14
|
|
|
|
15
|
|
|
```go |
16
|
|
|
import ( |
17
|
|
|
"github.com/staskobzar/goagi" |
18
|
|
|
"log" |
19
|
|
|
) |
20
|
|
|
|
21
|
|
|
int main() { |
22
|
|
|
agi, err := goagi.NewAGI() |
23
|
|
|
if err != nil { |
24
|
|
|
log.Fatalln(err) |
25
|
|
|
} |
26
|
|
|
agi.Verbose("New AGI session.") |
27
|
|
|
if err := agi.SetMusic("on", "jazz"); err != nil { |
28
|
|
|
log.Fatalln(err) |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
clid, err := agi.GetVariable("CALLERID") |
32
|
|
|
if err != nil { |
33
|
|
|
log.Fatalln(err) |
34
|
|
|
} |
35
|
|
|
agi.Verbose("Call from " + clid) |
36
|
|
|
if err := agi.SetMusic("off"); err != nil { |
37
|
|
|
log.Fatalln(err) |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
``` |
41
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
func NewAGI() (*AGI, error) { |
44
|
|
|
in := bufio.NewWriter(os.Stdout) |
45
|
|
|
out := bufio.NewReader(os.Stdin) |
46
|
|
|
|
47
|
|
|
agi, err := newInterface(bufio.NewReadWriter(out, in)) |
48
|
|
|
if err != nil { |
49
|
|
|
return nil, err |
50
|
|
|
} |
51
|
|
|
return agi, nil |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// FastAGI defines sturcture of fast AGI server |
55
|
|
|
type FastAGI struct { |
56
|
|
|
agi *AGI |
57
|
|
|
conn net.Conn |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Conn returns AGI instance on every Asterisk connection |
|
|
|
|
61
|
|
|
func (fagi *FastAGI) AGI() *AGI { |
62
|
|
|
return fagi.agi |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Close terminates Fast AGI |
66
|
|
|
func (fagi *FastAGI) Close() { |
67
|
|
|
fagi.conn.Close() |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// RemoteAddr returns remote connected client host and port as string |
71
|
|
|
func (fagi *FastAGI) RemoteAddr() string { |
72
|
|
|
return fagi.conn.RemoteAddr().String() |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/* |
76
|
|
|
NewFastAGI starts listening and serve AGI network calls. |
77
|
|
|
|
78
|
|
|
Usage example: |
79
|
|
|
|
80
|
|
|
```go |
81
|
|
|
import ( |
82
|
|
|
"github.com/staskobzar/goagi" |
83
|
|
|
"time" |
84
|
|
|
"log" |
85
|
|
|
) |
86
|
|
|
|
87
|
|
|
func main() { |
88
|
|
|
serve := func (fagi *goagi.FastAGI) { |
89
|
|
|
agi := fagi.AGI() |
90
|
|
|
agi.Verbose("New FastAGI session") |
91
|
|
|
agi.Answer() |
92
|
|
|
if clid, err := agi.GetVariable("CALLERID"); err == nil { |
93
|
|
|
log.Printf("CallerID %s\n", clid) |
94
|
|
|
ag.Varbose("Call from " + clid) |
95
|
|
|
} |
96
|
|
|
fagi.Close() |
97
|
|
|
} |
98
|
|
|
// listen, serve and reconnect on fail |
99
|
|
|
for { |
100
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:4573") |
101
|
|
|
if err != nil { |
102
|
|
|
log.Println("Connection error. Re-try in 3 sec.") |
103
|
|
|
<-time.After(time.Second * 3) |
104
|
|
|
continue |
105
|
|
|
} |
106
|
|
|
chFagi, chErr := goagi.NewFastAGI(ln) |
107
|
|
|
|
108
|
|
|
Loop: |
109
|
|
|
for { |
110
|
|
|
select { |
111
|
|
|
case fagi := <-chFagi: |
112
|
|
|
go serve(fagi) |
113
|
|
|
case err :=<-chErr: |
114
|
|
|
ln.Close() |
115
|
|
|
log.Println(err) |
116
|
|
|
break Loop |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
``` |
122
|
|
|
*/ |
123
|
|
|
func NewFastAGI(ln net.Listener) (<-chan *FastAGI, <-chan error) { |
124
|
|
|
chFagi := make(chan *FastAGI) |
125
|
|
|
chErr := make(chan error) |
126
|
|
|
|
127
|
|
|
go func(chFagi chan *FastAGI, chErr chan error) { |
128
|
|
|
defer close(chFagi) |
129
|
|
|
defer close(chErr) |
130
|
|
|
for { |
131
|
|
|
conn, err := ln.Accept() |
132
|
|
|
if err != nil { |
133
|
|
|
chErr <- err |
134
|
|
|
return |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
in := bufio.NewWriter(conn) |
138
|
|
|
out := bufio.NewReader(conn) |
139
|
|
|
agi, err := newInterface(bufio.NewReadWriter(out, in)) |
140
|
|
|
if err != nil { |
141
|
|
|
// invalid input data. skip |
142
|
|
|
conn.Close() |
143
|
|
|
continue |
144
|
|
|
} |
145
|
|
|
fagi := &FastAGI{agi: agi, conn: conn} |
146
|
|
|
chFagi <- fagi |
147
|
|
|
} |
148
|
|
|
}(chFagi, chErr) |
149
|
|
|
return chFagi, chErr |
150
|
|
|
} |
151
|
|
|
|