Skip to content

Commit 8980ab8

Browse files
committed
Basic sms send configuration complete
1 parent fba08d6 commit 8980ab8

File tree

7 files changed

+54
-53
lines changed

7 files changed

+54
-53
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: 34 additions & 37 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, headers ...http.Header) *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,8 +224,7 @@ 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
222230
t := time.Now()
@@ -226,24 +234,13 @@ func (m *zte8810ft) SendSMS(phone string, message string) error {
226234
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-
request.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ require (
2020
github.com/leodido/go-urn v1.4.0 // indirect
2121
github.com/magiconair/properties v1.8.7 // indirect
2222
github.com/mitchellh/mapstructure v1.5.0 // indirect
23-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
2423
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
2524
github.com/sagikazarmark/locafero v0.4.0 // indirect
2625
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
@@ -29,14 +28,14 @@ require (
2928
github.com/spf13/cast v1.6.0 // indirect
3029
github.com/spf13/pflag v1.0.5 // indirect
3130
github.com/subosito/gotenv v1.6.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
3535
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
3636
golang.org/x/net v0.23.0 // indirect
3737
golang.org/x/sys v0.18.0 // indirect
3838
golang.org/x/text v0.14.0 // indirect
39-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
4039
gopkg.in/ini.v1 v1.67.0 // indirect
4140
gopkg.in/yaml.v3 v3.0.1 // indirect
4241
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
6969
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
7070
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
7171
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
72+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
7273
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7374
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
7475
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7576
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
7677
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7778
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
7879
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
80+
github.com/warthog618/sms v0.3.0 h1:LYAb5ngmu2qjNExgji3B7xi2tIZ9+DsuE9pC5xs4wwc=
81+
github.com/warthog618/sms v0.3.0/go.mod h1:+bYZGeBxu003sxD5xhzsrIPBAjPBzTABsRTwSpd7ld4=
7982
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
8083
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
8184
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
@@ -95,6 +98,8 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8X
9598
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9699
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
97100
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
101+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
102+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
98103
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
99104
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
100105
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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)