|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/zlib" |
| 6 | + "dextercai.com/feeyo-adsb-golang/conf" |
| 7 | + "dextercai.com/feeyo-adsb-golang/constant" |
| 8 | + "dextercai.com/feeyo-adsb-golang/log" |
| 9 | + "encoding/base64" |
| 10 | + "fmt" |
| 11 | + "io/ioutil" |
| 12 | + "net" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +var err error |
| 20 | + |
| 21 | +const eachPackage = 8192 |
| 22 | +const thisLogFlag = "main" |
| 23 | + |
| 24 | +func main() { |
| 25 | + fmt.Println("项目地址: https://github.com/dextercai/feeyo-adsb-golang 基于GPL3.0协议进行开源") |
| 26 | + fmt.Printf("当前版本:%s,编译时间:%s", constant.Version, constant.BuildTime) |
| 27 | + fmt.Println("") |
| 28 | + fmt.Println("敬告: 请不要尝试将相关电波数据传送至FR24, RadarBox, FA等境外平台, 这将严重违反无线电管理条例以及国家安全法!") |
| 29 | + fmt.Println("=============================================================================================") |
| 30 | + conf.ParseConf() |
| 31 | + if conf.GlobalConfig.UUID == "" || |
| 32 | + len(conf.GlobalConfig.UUID) != 16 || |
| 33 | + conf.GlobalConfig.IpDump1090 == "" || |
| 34 | + conf.GlobalConfig.PortDump1090 == "" || |
| 35 | + conf.GlobalConfig.FeeyoUrl == "" { |
| 36 | + |
| 37 | + log.Logger.Fatalf("配置中存在错误") |
| 38 | + } |
| 39 | + for { |
| 40 | + dump1090Conn, err := net.Dial("tcp", conf.GlobalConfig.IpDump1090+":"+conf.GlobalConfig.PortDump1090) |
| 41 | + if err != nil { |
| 42 | + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "连接到Dump1090失败", err.Error()) |
| 43 | + log.Logger.Printf("[%s]:%s", thisLogFlag, "15秒后重试") |
| 44 | + time.Sleep(15 * time.Second) |
| 45 | + continue |
| 46 | + } else { |
| 47 | + log.Logger.Printf("[%s]:%s", thisLogFlag, "连接到Dump1090成功") |
| 48 | + } |
| 49 | + var buf [eachPackage]byte |
| 50 | + for { |
| 51 | + read, err := dump1090Conn.Read(buf[0:]) |
| 52 | + if err != nil { |
| 53 | + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "读取数据错误", err.Error()) |
| 54 | + _ = dump1090Conn.Close() |
| 55 | + log.Logger.Printf("[%s]:%s", thisLogFlag, "已断开连接,正尝试重连") |
| 56 | + break |
| 57 | + } else { |
| 58 | + if buf[read-1] == 10 { |
| 59 | + sendMessage(buf[0:read]) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +func sendMessage(line []byte) { |
| 68 | + sourceData := base64.StdEncoding.EncodeToString(DoZlibCompress(line)) |
| 69 | + postValue := url.Values{} |
| 70 | + postValue.Set("from", conf.GlobalConfig.UUID) |
| 71 | + postValue.Set("code", sourceData) |
| 72 | + resp, err := http.Post(conf.GlobalConfig.FeeyoUrl, "application/x-www-form-urlencoded", strings.NewReader(postValue.Encode())) |
| 73 | + if err != nil { |
| 74 | + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "上传错误", err.Error()) |
| 75 | + return |
| 76 | + } |
| 77 | + defer resp.Body.Close() |
| 78 | + body, err := ioutil.ReadAll(resp.Body) |
| 79 | + if err != nil { |
| 80 | + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "上传错误", err.Error()) |
| 81 | + } else { |
| 82 | + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "上传成功", string(body)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func DoZlibCompress(src []byte) []byte { |
| 87 | + var in bytes.Buffer |
| 88 | + w := zlib.NewWriter(&in) |
| 89 | + _, _ = w.Write(src) |
| 90 | + _ = w.Close() |
| 91 | + return in.Bytes() |
| 92 | +} |
| 93 | + |
| 94 | +/* |
| 95 | +func DoZlibUnCompress(compressSrc []byte) []byte { |
| 96 | + b := bytes.NewReader(compressSrc) |
| 97 | + var out bytes.Buffer |
| 98 | + r, _ := zlib.NewReader(b) |
| 99 | + _, _ = io.Copy(&out, r) |
| 100 | + return out.Bytes() |
| 101 | +} |
| 102 | +
|
| 103 | +*/ |
0 commit comments