Skip to content

Commit 760d3f8

Browse files
authored
Merge pull request #1 from brokenCursor/remote-debug
SMS & remote connection
2 parents eb3d348 + 8980ab8 commit 760d3f8

File tree

7 files changed

+54
-58
lines changed

7 files changed

+54
-58
lines changed

args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type (
2222
BaseArgs struct {
2323
Connection *ConnectionArgs `validate:"-" arg:"subcommand:conn" help:"Manage cell connection"`
2424
SMS *SMSActionArgs `validate:"-" arg:"subcommand:sms" help:"Manage SMS"`
25-
Ip string `validate:"omitempty,ipv4" arg:"--ip" help:"Override IP in config file"`
25+
Host string `validate:"omitempty,ipv4|hostname" arg:"--host" help:"Override hostname in config file"`
2626
DisableColor bool `arg:"-p,--plain" help:"Disable color for better software interaction"`
2727
}
2828
)

drivers/8810ft.go

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99
"strconv"
1010
"strings"
1111
"time"
12+
13+
"github.com/warthog618/sms/encoding/gsm7"
1214
)
1315

1416
// DO NOT USE DIRECTLY
1517
type (
1618
zte8810ft struct {
17-
ip string
19+
host string
1820
basePath string
1921
}
2022

@@ -34,33 +36,32 @@ func init() {
3436
registerDriver("ZTE 8810FT", newZTE8810FT)
3537
}
3638

37-
func newZTE8810FT(ip string) BaseModem {
38-
return &zte8810ft{ip: ip, basePath: "/goform/goform_set_cmd_process"}
39+
func newZTE8810FT(host string) BaseModem {
40+
return &zte8810ft{host: host, basePath: "/goform/goform_set_cmd_process"}
3941
}
4042

4143
func (m *zte8810ft) getBaseURL(path string) *url.URL {
42-
return &url.URL{Scheme: "http", Host: m.ip, Path: path}
44+
return &url.URL{Scheme: "http", Host: m.host, Path: path}
4345
}
4446

45-
func (m *zte8810ft) getNewRequest(method string, url *url.URL) *http.Request {
47+
func (m *zte8810ft) getNewRequest(method string, url *url.URL, headers http.Header) *http.Request {
48+
headers.Add("Referer", fmt.Sprintf("http://%s/index.html", m.host))
49+
4650
return &http.Request{
4751
Proto: "HTTP/1.1",
4852
Method: method,
4953
URL: url,
50-
Header: http.Header{
51-
"Referer": {fmt.Sprintf("http://%s/index.html", m.ip)},
52-
"Content"
53-
},
54+
Header: headers,
5455
}
5556
}
5657

57-
func (m *zte8810ft) SetTargetIP(ip string) error {
58-
m.ip = ip
58+
func (m *zte8810ft) SetHost(ip string) error {
59+
m.host = ip
5960
return nil
6061
}
6162

62-
func (m *zte8810ft) GetTargetIP() string {
63-
return m.ip
63+
func (m *zte8810ft) GetHost() string {
64+
return m.host
6465
}
6566

6667
func (m *zte8810ft) GetModel() string {
@@ -74,7 +75,7 @@ func (m *zte8810ft) ConnectCell() error {
7475
query := u.Query()
7576
query.Add("goformId", "CONNECT_NETWORK")
7677
u.RawQuery = query.Encode()
77-
request := m.getNewRequest("GET", u)
78+
request := m.getNewRequest("GET", u, http.Header{})
7879

7980
resp, err := httpClient.Do(request)
8081

@@ -112,7 +113,7 @@ func (m *zte8810ft) DisconnectCell() error {
112113
query := u.Query()
113114
query.Add("goformId", "DISCONNECT_NETWORK")
114115
u.RawQuery = query.Encode()
115-
request := m.getNewRequest("GET", u)
116+
request := m.getNewRequest("GET", u, http.Header{})
116117

117118
resp, err := httpClient.Do(request)
118119
// Process errors
@@ -157,7 +158,7 @@ func (m *zte8810ft) GetCellConnStatus() (*LinkStatus, error) {
157158
query.Add("_", strconv.FormatInt((time.Now().UnixMilli)(), 10))
158159
u.RawQuery = query.Encode()
159160

160-
request := m.getNewRequest("GET", u)
161+
request := m.getNewRequest("GET", u, http.Header{})
161162

162163
resp, err := httpClient.Do(request)
163164

@@ -201,12 +202,20 @@ func (m *zte8810ft) SendSMS(phone string, message string) error {
201202
// GET /goform/goform_set_cmd_process?goformId=SEND_SMS
202203
// Prepare everything to make a request
203204

204-
// Encode message into GSM-7
205-
// encodedMsg, err := gsm7.Encode([]byte(message))
206-
// if err != nil {
207-
// return ActionError{"sms send", err}
208-
// }
205+
// Encode message into GSM-7 as byte array
206+
encodedMsg, err := gsm7.Encode([]byte(message))
207+
switch err {
208+
209+
}
210+
if err != nil {
211+
return ActionError{"sms send", err}
212+
}
209213

214+
// Format into proprietary two byte format: 1122 (0074)
215+
formattedMsg := ""
216+
for _, hex := range encodedMsg {
217+
formattedMsg += fmt.Sprintf("00%X", hex)
218+
}
210219
u := m.getBaseURL("/goform/goform_set_cmd_process")
211220

212221
// Build body
@@ -215,35 +224,23 @@ func (m *zte8810ft) SendSMS(phone string, message string) error {
215224
query.Add("ID", "-1")
216225
query.Add("encode_type", "GSM7_default")
217226
query.Add("Number", phone)
218-
// query.Add("MessageBody", fmt.Sprintf("%X", encodedMsg))
219-
query.Add("MessageBody", "0074006500730074")
227+
query.Add("MessageBody", formattedMsg)
220228

221229
// Build send timestamp
222-
currTime := time.Now()
223-
if _, tz := currTime.Zone(); tz >= 0 {
224-
query.Add("sms_time", currTime.Format("06;01;02;15;04;05;+")+strconv.Itoa(tz/3600))
230+
t := time.Now()
231+
if _, tz := t.Zone(); tz >= 0 {
232+
query.Add("sms_time", t.Format("06;01;02;15;04;05;+")+strconv.Itoa(tz/3600))
225233
} else {
226-
query.Add("sms_time", currTime.Format("06;01;02;15;04;05;")+strconv.Itoa(tz/3600))
234+
query.Add("sms_time", t.Format("06;01;02;15;04;05;")+strconv.Itoa(tz/3600))
227235
}
228236

229-
// data := map[string]string{
230-
// "goformId": "SEND_SMS",
231-
// "Number": phone,
232-
// "sms_time": time.Now().Format("02;01;06;15;04;05;-07"),
233-
// "MessageBody": string(encodedMsg),
234-
// "ID": "-1",
235-
// "encode_type": "GSM7_default",
236-
// }
237-
238-
request := m.getNewRequest("POST", u)
237+
request := m.getNewRequest("POST", u, http.Header{
238+
"Content-Type": {"application/x-www-form-urlencoded", "charset=UTF-8"}})
239239

240240
// Some Go-level string manipulation
241-
fmt.Println(query.Encode())
242-
// stringReader := strings.NewReader(query.Encode())
243-
stringReader := strings.NewReader("goformId=SEND_SMS&Number=%2B79124446729&sms_time=24%3B07%3B28%3B19%3B01%3B24%3B%2B4&MessageBody=0074006500730074&ID=-1&encode_type=GSM7_default")
241+
stringReader := strings.NewReader(query.Encode())
244242
stringReadCloser := io.NopCloser(stringReader)
245243
request.Body = stringReadCloser
246-
247244

248245
resp, err := httpClient.Do(request)
249246

drivers/common.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import "fmt"
66

77
type (
88
BaseModem interface {
9-
SetTargetIP(ip string) error
10-
GetTargetIP() string
9+
SetHost(host string) error
10+
GetHost() string
1111
GetModel() string
1212
}
1313

@@ -42,7 +42,7 @@ type (
4242
}
4343
)
4444

45-
var drivers map[string]func(ip string) BaseModem = map[string]func(ip string) BaseModem{}
45+
var drivers map[string]func(host string) BaseModem = map[string]func(host string) BaseModem{}
4646

4747
func isRegistered(name string) bool {
4848
// Check if driver has already been registered
@@ -60,12 +60,12 @@ func registerDriver(name string, generator func(ip string) BaseModem) {
6060
drivers[name] = generator
6161
}
6262

63-
func GetModemDriver(model string, ip string) (BaseModem, error) {
63+
func GetModemDriver(model string, host string) (BaseModem, error) {
6464
if !isRegistered(model) {
6565
return nil, ErrUnknownModel
6666
}
6767

68-
return drivers[model](ip), nil
68+
return drivers[model](host), nil
6969
}
7070

7171
func GetAvailableDrivers() *[]string {

drivers/dummy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func (m *dummy) GetModel() string {
2020
return "Dummy"
2121
}
2222

23-
func (m *dummy) SetTargetIP(ip string) error {
23+
func (m *dummy) SetHost(ip string) error {
2424
m.ip = ip
2525
return nil
2626
}
2727

28-
func (m *dummy) GetTargetIP() string {
28+
func (m *dummy) GetHost() string {
2929
return m.ip
3030
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/spf13/cast v1.6.0 // indirect
2929
github.com/spf13/pflag v1.0.5 // indirect
3030
github.com/subosito/gotenv v1.6.0 // indirect
31-
github.com/warthog618/sms v0.3.0 // indirect
31+
github.com/warthog618/sms v0.3.0
3232
go.uber.org/atomic v1.9.0 // indirect
3333
go.uber.org/multierr v1.9.0 // indirect
3434
golang.org/x/crypto v0.21.0 // indirect

go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
4040
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
4141
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4242
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
43+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
4344
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
4445
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
4546
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
@@ -93,8 +94,6 @@ golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
9394
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
9495
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
9596
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
96-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
97-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9897
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
9998
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10099
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=

mcli.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func init() {
3636
config.AddConfigPath(dir + sep + "modem-cli")
3737

3838
config.SetDefault("modem.model", "dummy")
39-
config.SetDefault("modem.ip", "127.0.0.1")
39+
config.SetDefault("modem.host", "127.0.0.1")
4040

4141
err = config.ReadInConfig()
4242
if err != nil { // Handle errors reading the config file
@@ -57,20 +57,20 @@ func run() error {
5757
modemConfig := config.Sub("modem")
5858

5959
model := modemConfig.GetString("model")
60-
ip := modemConfig.GetString("ip")
60+
ip := modemConfig.GetString("host")
6161

6262
if err := validate.Struct(args); err != nil {
6363
// TODO: add actual error output
64-
parser.Fail("invalid value for \"--ip\" ")
64+
parser.Fail("invalid value for \"--host\" ")
6565
}
6666

6767
if args.DisableColor {
6868
cfmt.DisableColors()
6969
}
7070

7171
// If IP has been overridden
72-
if len(args.Ip) > 0 {
73-
ip = args.Ip
72+
if len(args.Host) > 0 {
73+
ip = args.Host
7474
}
7575

7676
modem, err := drivers.GetModemDriver(model, ip)
@@ -136,7 +136,7 @@ func run() error {
136136
parser.FailSubcommand("Unknown action", "sms")
137137
}
138138

139-
err = sms.SendSMS(args.SMS.Send.PhoneNumber, args.SMS.Send.PhoneNumber)
139+
err = sms.SendSMS(args.SMS.Send.PhoneNumber, args.SMS.Send.Message)
140140
if err != nil {
141141
return err
142142
}

0 commit comments

Comments
 (0)