-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreeg.go
263 lines (249 loc) · 5.13 KB
/
threeg.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/urfave/cli"
)
type ThreeG struct {
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
APN string `json:"apn"`
Dial string `json:"dial"`
Username string `json:"username"`
Password string `json:"password"`
DefaultGateway bool `json:"defaultGateway"`
}
func (c *ThreeG) WriteTo(out io.Writer) error {
cfgTpl := `
[Dialer Defaults]
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2
Modem Type = USB Modem
; Phone = <Target Phone Number>
Phone = {{.Dial}}
ISDN = 0
; Username = <Your Login Name>, 0 if not specified
Username = {{.Username}}
; Password = <Your Password>, 0 if not specified
Password = {{.Password}}
Init1 = ATZ
; modem command port (by IMSI)
Modem = /dev/{{.IMSI}}.imsi
Baud = 115200
; set APN
Init3 = AT+CGDCONT=1,"IP","{{.APN}}"
Stupid Mode = 1
`
tpl, err := template.New("cfg").Parse(cfgTpl)
if err != nil {
return err
}
ctx := *c
if ctx.Username == "" {
ctx.Username = "0"
}
if ctx.Password == "" {
ctx.Password = "0"
}
return tpl.Execute(out, ctx)
}
type ThreeGState struct {
Enabled bool `json:"enabled"`
Configg *ThreeG `json:"config"`
}
func ThreegCMD(ctx *cli.Context) error {
if ctx.IsSet(enableFlag) {
return EnableThreeg(ctx)
}
if ctx.IsSet(disableFlag) {
return DisableThreeg(ctx)
}
if ctx.IsSet(removeFlag) {
return RemoveThreeg(ctx)
}
if ctx.IsSet(configFlag) {
return configThreegCMD(ctx)
}
return nil
}
func configThreegCMD(ctx *cli.Context) error {
base := ctx.String("dir")
src := ctx.String("config")
if src == "" {
return errors.New("fconf: missing configuration source file")
}
var b []byte
var err error
if src == "stdin" {
b, err = ReadFromStdin()
if err != nil {
return err
}
} else {
b, err = ioutil.ReadFile(src)
if err != nil {
return err
}
}
e := ThreeG{}
err = json.Unmarshal(b, &e)
if err != nil {
return err
}
err = checkDir(base)
if err != nil {
return err
}
state := &ThreeGState{Configg: &e}
ms, err := threeGState(e.IMEI)
if err == nil {
state.Enabled = ms.Enabled
}
b, _ = json.Marshal(state)
setInterface(ctx, e.IMSI)
return keepState(
fmt.Sprintf(defaultThreeGGConfig, e.IMSI), b)
}
func threeGState(i string) (*ThreeGState, error) {
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
b, err := ioutil.ReadFile(filepath.Join(dir,
fmt.Sprintf(defaultThreeGGConfig, i)))
if err != nil {
return nil, err
}
f := &ThreeGState{}
err = json.Unmarshal(b, f)
if err != nil {
return nil, err
}
if f.Configg == nil {
return nil, ErrWrongStateFile
}
return f, nil
}
func EnableThreeg(ctx *cli.Context) error {
if ctx.IsSet(configFlag) {
err := configThreegCMD(ctx)
if err != nil {
return err
}
}
i := getInterface(ctx)
if i == "" {
return errors.New("missing imei , you must specify imei")
}
e, err := threeGState(i)
if err != nil {
return err
}
name := filepath.Join(apConfigBase, threeGService)
_, err = os.Stat(name)
if err == nil {
if !e.Enabled {
return errors.New("you can not enable two 3g networks")
}
}
var buf bytes.Buffer
err = e.Configg.WriteTo(&buf)
if err != nil {
return err
}
err = ioutil.WriteFile(name, buf.Bytes(), 0644)
if err != nil {
return err
}
fmt.Printf("written 3g config to %s\n", name)
service := "wvdial"
err = restartService(service)
if err != nil {
return fmt.Errorf("ERROR: restarting systemd %v ", err)
}
err = enableService(service)
if err != nil {
return fmt.Errorf("ERROR: enabling systemd %v ", err)
}
e.Enabled = true
data, err := json.Marshal(e)
if err != nil {
return err
}
fmt.Printf("successfully enabled 3g for %s \n", i)
return keepState(
fmt.Sprintf(defaultThreeGGConfig, i), data)
}
func DisableThreeg(ctx *cli.Context) error {
if ctx.IsSet(configFlag) {
fmt.Println("WARN: config flag will be ignored when diable flag is used")
}
i := getInterface(ctx)
if i == "" {
return errors.New("missing imei, you must specify imei")
}
e, err := threeGState(i)
if err != nil {
return err
}
e.Enabled = false
data, err := json.Marshal(e)
if err != nil {
return err
}
service := "wvdial"
err = stopService(service)
if err != nil {
return err
}
err = disableService(service)
if err != nil {
return err
}
name := filepath.Join(apConfigBase, threeGService)
err = removeFile(name)
if err != nil {
return err
}
fmt.Println("successfully disabled 3G for ", i)
return keepState(
fmt.Sprintf(defaultThreeGGConfig, i), data)
}
func RemoveThreeg(ctx *cli.Context) error {
i := getInterface(ctx)
if i == "" {
return errors.New("missing imei, you must specify imei")
}
e, err := threeGState(i)
if err != nil {
return err
}
if e.Enabled {
err = DisableThreeg(ctx)
if err != nil {
return err
}
}
// removestate file
stateFile := filepath.Join(stateDir(),
fmt.Sprintf(defaultThreeGGConfig, i))
err = removeFile(stateFile)
if err != nil {
return err
}
// remove config
unit := filepath.Join(apConfigBase, threeGService)
err = removeFile(unit)
if err != nil {
if !os.IsNotExist(err) {
return err
}
}
return nil
}