-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
178 lines (151 loc) · 4.82 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"flag"
"fmt"
"io"
"log"
"net/mail"
"os"
"strconv"
"github.com/sorokinmax/smpp"
"github.com/sorokinmax/smpp/pdu"
)
const version = "v1.7.2"
/*
type AppRegistry struct {
SessionId string
SystemId string
Password string
}*/
var (
serverAddr string
systemID string
msgID int
//appData map[string]AppRegistry
)
var cfg Config
func init() {
readConfigFile(&cfg)
Messages = make(map[byte]map[byte]Message)
}
func main() {
log.SetFlags(log.LstdFlags)
f, err := os.OpenFile("smpp-gateway.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
multi := io.MultiWriter(os.Stdout, f)
log.SetOutput(multi)
//appData = make(map[string]AppRegistry)
serverHost := cfg.SMPP.Host + ":" + strconv.Itoa(cfg.SMPP.Port)
flag.StringVar(&serverAddr, "addr", serverHost, "server will listen on this address.")
flag.StringVar(&systemID, "systemid", "SMS Gateway", "descriptive server identification.")
flag.Parse()
go CacheAutoUpdater("./mapping.txt")
sessConf := smpp.SessionConf{
Handler: smpp.HandlerFunc(func(ctx *smpp.Context) {
switch ctx.CommandID() {
case pdu.BindTransmitterID:
btx, err := ctx.BindTx()
if err != nil {
log.Printf("Invalid PDU in context error: %+v", err)
}
log.Printf("Incoming connection from %s with ID: %s", ctx.RemoteAddr(), btx.SystemID)
/*appData[ctx.SessionID()] = AppRegistry{
SessionId: ctx.SessionID(),
SystemId: btx.SystemID,
Password: btx.Password,
}*/
resp := btx.Response(systemID)
responseStatus := pdu.StatusInvPaswd
if (btx.Password == cfg.SMPP.Password && btx.SystemID == cfg.SMPP.User) || cfg.SMPP.User == "" {
responseStatus = pdu.StatusOK
}
if err := ctx.Respond(resp, responseStatus); err != nil {
log.Printf("Server can't respond to the Binding request: %+v", err)
}
case pdu.BindTransceiverID:
btrx, err := ctx.BindTRx()
if err != nil {
log.Printf("Invalid PDU in context error: %+v", err)
}
log.Printf("Incoming connection from %s with ID: %s", ctx.RemoteAddr(), btrx.SystemID)
/*appData[ctx.SessionID()] = AppRegistry{
SessionId: ctx.SessionID(),
SystemId: btrx.SystemID,
Password: btrx.Password,
}*/
resp := btrx.Response(systemID)
responseStatus := pdu.StatusInvPaswd
if (btrx.Password == cfg.SMPP.Password && btrx.SystemID == cfg.SMPP.User) || cfg.SMPP.User == "" {
responseStatus = pdu.StatusOK
}
if err := ctx.Respond(resp, responseStatus); err != nil {
log.Printf("Server can't respond to the Binding request: %+v", err)
}
case pdu.SubmitSmID:
var response string
var status pdu.Status
sm, err := ctx.SubmitSm()
if err != nil {
log.Printf("Invalid PDU in context error: %+v", err)
}
message := GetMessage(sm.ShortMessage)
switch sm.DataCoding {
case 8:
message = UCS2Decode(message)
}
if len(message) > 0 {
log.Printf("Incoming SMS\n\tSessionId: %s\n\tFrom: %s\n\tTo:%s\n\tPriority: %s\n\tRemoteAddress: %s\n\tSMS: %s", ctx.SessionID(), sm.SourceAddr, sm.DestinationAddr, strconv.Itoa(sm.PriorityFlag), ctx.RemoteAddr(), message)
if len(cache.Mapping[sm.DestinationAddr]) > 0 {
log.Printf("Send SMS to: %s", cache.Mapping[sm.DestinationAddr])
// Check dst type
_, err := mail.ParseAddress(cache.Mapping[sm.DestinationAddr])
if err == nil {
response, status = SendEmail(sm, ctx, message)
} else {
response, status = SendTelegram(sm, ctx, message)
}
} else {
log.Printf("msgID %d: address not matched.", msgID)
response = fmt.Sprintf("msgID_%d: address not matched.", msgID)
status = pdu.StatusDeliveryFailure
}
}
resp := sm.Response(response)
if err := ctx.Respond(resp, status); err != nil {
log.Printf("Server can't respond to the submit_sm request: %+v", err)
}
msgID++
case pdu.EnquireLinkID:
el, err := ctx.EnquireLink()
if err != nil {
log.Printf("Invalid PDU in context error: %+v", err)
}
resp := el.Response()
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
log.Printf("Server can't respond to the enquire_link request: %+v", err)
}
case pdu.UnbindID:
unb, err := ctx.Unbind()
if err != nil {
log.Printf("Invalid PDU in context error: %+v", err)
}
//delete(appData, ctx.SessionID())
resp := unb.Response()
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
log.Printf("Server can't respond to the submit_sm request: %+v", err)
}
ctx.CloseSession()
}
}),
}
srv := smpp.NewServer(serverAddr, sessConf)
log.Printf("'%s' is listening on '%s'", systemID, serverAddr)
err = srv.ListenAndServe()
if err != nil {
log.Printf("Serving exited with error: %+v", err)
}
log.Print("Server closed")
}