-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathap.go
206 lines (196 loc) · 3.95 KB
/
ap.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli"
)
type AccessPointState struct {
Enabled bool `json:"enabled"`
Configg *AccessPointConfig `json:"config"`
}
func ApCMD(ctx *cli.Context) error {
if ctx.IsSet(enableFlag) {
return EnableApCMD(ctx)
}
if ctx.IsSet(disableFlag) {
return DisableApCMD(ctx)
}
if ctx.IsSet(removeFlag) {
return RemoveApCMD(ctx)
}
if ctx.IsSet(configFlag) {
return ConfigApCMD(ctx)
}
return nil
}
func ConfigApCMD(ctx *cli.Context) error {
base := ctx.String("dir")
name := ctx.String("name")
src := ctx.String("config")
if src == "" {
return errors.New("fconf: missing argument")
}
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 := &AccessPointConfig{}
err = json.Unmarshal(b, e)
if err != nil {
return err
}
err = checkDir(base)
if err != nil {
return err
}
ap := DefaultAccesPoint()
ap.Update(e)
if strings.Contains(name, "%s") {
name = fmt.Sprintf(name, ap.WifiIface)
}
filename := filepath.Join(base, name)
var buf bytes.Buffer
_, err = ap.WriteTo(&buf)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, buf.Bytes(), 0644)
if err != nil {
return err
}
fmt.Printf("successful written access point configuration to %s \n", filename)
state := &AccessPointState{Configg: ap.State()}
as, err := accessPointState(ap.WifiIface)
if err == nil {
state.Enabled = as.Enabled
}
data, err := json.Marshal(state)
if err != nil {
return err
}
ctx.GlobalSet("interface", ap.WifiIface)
return keepState(
fmt.Sprintf(defaultAccessPointConfig, ap.WifiIface), data)
}
func accessPointState(i string) (*AccessPointState, error) {
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
b, err := ioutil.ReadFile(filepath.Join(dir,
fmt.Sprintf(defaultAccessPointConfig, i)))
if err != nil {
return nil, err
}
a := &AccessPointState{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
if a.Configg == nil {
return nil, ErrWrongStateFile
}
return a, nil
}
func EnableApCMD(ctx *cli.Context) error {
if ctx.IsSet(configFlag) {
err := ConfigApCMD(ctx)
if err != nil {
return err
}
}
i := getInterface(ctx)
if i == "" {
return errors.New("missing interface, you must specify interface")
}
state, err := accessPointState(i)
if err != nil {
return err
}
service := "create_ap@" + i
err = startService(service)
if err != nil {
return err
}
err = enableService(service)
if err != nil {
return err
}
state.Enabled = true
data, err := json.Marshal(state)
if err != nil {
return err
}
return keepState(
fmt.Sprintf(defaultAccessPointConfig, i), data)
}
func DisableApCMD(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 interface, you must specify interface")
}
state, err := accessPointState(i)
if err != nil {
return err
}
service := "create_ap@" + i
err = stopService(service)
if err != nil {
return err
}
err = disableService(service)
if err != nil {
return err
}
state.Enabled = false
data, err := json.Marshal(state)
if err != nil {
return err
}
return keepState(
fmt.Sprintf(defaultAccessPointConfig, i), data)
}
func RemoveApCMD(ctx *cli.Context) error {
i := getInterface(ctx)
if i == "" {
return errors.New("missing interface, you must specify interface")
}
a, err := accessPointState(i)
if err != nil {
return err
}
if a.Enabled {
err = DisableApCMD(ctx)
if err != nil {
return err
}
}
// remove the state file
stateFile := filepath.Join(stateDir(),
fmt.Sprintf(defaultAccessPointConfig, i))
err = removeFile(stateFile)
if err != nil {
if !os.IsNotExist(err) {
return err
}
}
return nil
}