-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
105 lines (88 loc) · 1.93 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
package main
import (
"airkiss"
"errors"
"flag"
"fmt"
"math/rand"
"net"
"time"
)
func SmartConfigWithTimeout(codePackage []uint16, timeout time.Duration) error {
timeoutQuit := make(chan int, 1)
sucQuit := make(chan int, 1)
// 退出方式: 1. 超时退出 2. 收到反馈退出
go func() {
select {
case <-time.After(timeout):
timeoutQuit <- 1
return
}
}()
txAddr := &net.UDPAddr{IP: net.ParseIP("255.255.255.255"), Port: 10001}
rxAddr := &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 10000}
go func() {
rxData := make([]byte, 1024)
listernRx, err := net.ListenUDP("udp", rxAddr)
if err != nil {
fmt.Println(err)
return
}
for {
rxLen, remoteAddr, err := listernRx.ReadFromUDP(rxData)
if err != nil {
fmt.Println(err)
}
fmt.Println("remoteAddr: ", remoteAddr)
if rxLen > 0 {
if rxData[0] == gRandom {
sucQuit <- 1
return
}
}
}
}()
listernTx, err := net.ListenUDP("udp", txAddr)
if err != nil {
fmt.Println(err)
return err
}
for {
for _, dataLen := range codePackage {
_, err := listernTx.WriteToUDP(make([]byte, dataLen), txAddr)
if err != nil {
fmt.Println(err)
return err
}
time.Sleep(time.Millisecond * 10)
}
select {
case <-sucQuit:
return nil
case <-timeoutQuit:
return errors.New("smart config fail")
default:
}
}
}
var pwd = flag.String("p", "", "passwd")
var ssid = flag.String("e", "", "essid")
var timeout = flag.Int("t", 1000, "timeout")
var gRandom uint8
func main() {
flag.Parse()
if *pwd == "" || *ssid == "" {
fmt.Println("Please use -help to get input parameter requirements")
return
}
rand.Seed(time.Now().Unix())
gRandom = uint8(rand.Intn(127))
air := airkiss.New(*ssid, *pwd, gRandom)
UdpPackage := air.GreateCodePackage()
err := SmartConfigWithTimeout(UdpPackage, time.Second*time.Duration(*timeout))
if err != nil {
fmt.Println(err)
} else {
fmt.Println("smart config successful")
}
}