diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..0032fce --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,44 @@ +name: Release +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Get latest go version + id: version + run: | + echo ::set-output name=go_version::$(curl -s https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json | grep -oE '"version": "[0-9]{1}.[0-9]{1,}(.[0-9]{1,})?"' | head -1 | cut -d':' -f2 | sed 's/ //g; s/"//g') + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: ${{ steps.version.outputs.go_version }} + + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: Cache go module + uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + #- name: Get dependencies, run test + # run: | + # go test ./... + + - name: Build + if: startsWith(github.ref, 'refs/tags/') + env: + NAME: feeyo-adsb-golang + BINDIR: bin + run: make -j releases + + - name: Upload Release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + files: bin/* + draft: true \ No newline at end of file diff --git a/README.md b/README.md index 5cf3832..16e426b 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,15 @@ ## 使用说明 -本项目已更新,修改为Golang 1.11起支持的Go modules。 -主分支去除了UUID生成器的代码,但依旧保留二进制版本,请自定义UUID时确保不与其他人冲突。 - 由于本项目不包括Dump1090,也不限制SBS服务是否运行在本机,因此你可能需要首先安装Dump1090,具体细节可自行搜索,当然你也可以在本项目提一个Issue,我将很乐意为你解答。 -你需要编辑conf.ini文件 +如果你不具备编译条件,可以直接前往[本项目发布页](https://github.com/dextercai/feeyo-adsb-golang/releases)下载使用。 + +具有两种配置方式 + +### 一般文件模式(默认) + +你需要在程序**同目录**创建conf.ini文件,内容如下。 ``` [config] @@ -23,7 +26,31 @@ url=http://adsb.feeyo.com/adsb/ReceiveCompressADSB.php 以上展现的是dump1090运行在本机的情况,你也可以按照实际情况进行填写。 -如果你不具备编译条件,可以直接前往[本项目发布页](https://github.com/dextercai/feeyo-adsb-golang/releases)下载使用。 +### 命令行模式(进阶) + +若对终端操作较为熟悉,可使用该方式。 + +``` +Usage of ./adsb: + -conf string + conf文件位置 (default "./conf.ini") + -feeyo-url string + 飞常准接口地址 (default "https://adsb.feeyo.com/adsb/ReceiveCompressADSB.php") + -ip string + dump1090服务IP (default "127.0.0.1") + -port string + dump1090服务端口 (default "30003") + -use-file + 是否使用conf文件作为配置来源 (default true) + -uuid string + UUID 16位 +``` + +## TODO +- 统计、集成地图 +- 集成部分dump1090功能 +- webhook + ## 其他 diff --git a/adsb.go b/adsb.go new file mode 100644 index 0000000..4efeb6d --- /dev/null +++ b/adsb.go @@ -0,0 +1,103 @@ +package main + +import ( + "bytes" + "compress/zlib" + "dextercai.com/feeyo-adsb-golang/conf" + "dextercai.com/feeyo-adsb-golang/constant" + "dextercai.com/feeyo-adsb-golang/log" + "encoding/base64" + "fmt" + "io/ioutil" + "net" + "net/http" + "net/url" + "strings" + "time" +) + +var err error + +const eachPackage = 8192 +const thisLogFlag = "main" + +func main() { + fmt.Println("项目地址: https://github.com/dextercai/feeyo-adsb-golang 基于GPL3.0协议进行开源") + fmt.Printf("当前版本:%s,编译时间:%s", constant.Version, constant.BuildTime) + fmt.Println("") + fmt.Println("敬告: 请不要尝试将相关电波数据传送至FR24, RadarBox, FA等境外平台, 这将严重违反无线电管理条例以及国家安全法!") + fmt.Println("=============================================================================================") + conf.ParseConf() + if conf.GlobalConfig.UUID == "" || + len(conf.GlobalConfig.UUID) != 16 || + conf.GlobalConfig.IpDump1090 == "" || + conf.GlobalConfig.PortDump1090 == "" || + conf.GlobalConfig.FeeyoUrl == "" { + + log.Logger.Fatalf("配置中存在错误") + } + for { + dump1090Conn, err := net.Dial("tcp", conf.GlobalConfig.IpDump1090+":"+conf.GlobalConfig.PortDump1090) + if err != nil { + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "连接到Dump1090失败", err.Error()) + log.Logger.Printf("[%s]:%s", thisLogFlag, "15秒后重试") + time.Sleep(15 * time.Second) + continue + } else { + log.Logger.Printf("[%s]:%s", thisLogFlag, "连接到Dump1090成功") + } + var buf [eachPackage]byte + for { + read, err := dump1090Conn.Read(buf[0:]) + if err != nil { + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "读取数据错误", err.Error()) + _ = dump1090Conn.Close() + log.Logger.Printf("[%s]:%s", thisLogFlag, "已断开连接,正尝试重连") + break + } else { + if buf[read-1] == 10 { + sendMessage(buf[0:read]) + } + } + } + } + +} + +func sendMessage(line []byte) { + sourceData := base64.StdEncoding.EncodeToString(DoZlibCompress(line)) + postValue := url.Values{} + postValue.Set("from", conf.GlobalConfig.UUID) + postValue.Set("code", sourceData) + resp, err := http.Post(conf.GlobalConfig.FeeyoUrl, "application/x-www-form-urlencoded", strings.NewReader(postValue.Encode())) + if err != nil { + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "上传错误", err.Error()) + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "上传错误", err.Error()) + } else { + log.Logger.Printf("[%s]:%s\t%s", thisLogFlag, "上传成功", string(body)) + } +} + +func DoZlibCompress(src []byte) []byte { + var in bytes.Buffer + w := zlib.NewWriter(&in) + _, _ = w.Write(src) + _ = w.Close() + return in.Bytes() +} + +/* +func DoZlibUnCompress(compressSrc []byte) []byte { + b := bytes.NewReader(compressSrc) + var out bytes.Buffer + r, _ := zlib.NewReader(b) + _, _ = io.Copy(&out, r) + return out.Bytes() +} + +*/ diff --git a/conf/fileProvider.go b/conf/fileProvider.go new file mode 100644 index 0000000..0d5cc9d --- /dev/null +++ b/conf/fileProvider.go @@ -0,0 +1,32 @@ +package conf + +import ( + "dextercai.com/feeyo-adsb-golang/log" + "github.com/Unknwon/goconfig" +) + +type TConf struct { + IpDump1090 string + PortDump1090 string + FeeyoUrl string + UUID string +} + +var GlobalConfig TConf +var Config *goconfig.ConfigFile + +func reloadConfig(confLoc string) { + var err error + Config, err = goconfig.LoadConfigFile(confLoc) + if err != nil { + log.Logger.Fatalf("[Fatal]:conf.ini配置文件不存在,请检查.") + } + GlobalConfig.UUID, err = Config.GetValue("config", "UUID") + GlobalConfig.IpDump1090, err = Config.GetValue("config", "ip") + GlobalConfig.PortDump1090, err = Config.GetValue("config", "port") + GlobalConfig.FeeyoUrl, err = Config.GetValue("config", "url") + + if err != nil { + log.Logger.Fatalf("[Fatal]:解析配置时出现错误") + } +} diff --git a/conf/flagProvider.go b/conf/flagProvider.go new file mode 100644 index 0000000..68196d8 --- /dev/null +++ b/conf/flagProvider.go @@ -0,0 +1,27 @@ +package conf + +import ( + "dextercai.com/feeyo-adsb-golang/log" + "flag" +) + +var useFile bool +var confFile string + +func ParseConf() { + flag.StringVar(&GlobalConfig.UUID, "uuid", "", "UUID 16位") + flag.StringVar(&GlobalConfig.IpDump1090, "ip", "127.0.0.1", "dump1090服务IP") + flag.StringVar(&GlobalConfig.PortDump1090, "port", "30003", "dump1090服务端口") + flag.StringVar(&GlobalConfig.FeeyoUrl, "feeyo-url", "https://adsb.feeyo.com/adsb/ReceiveCompressADSB.php", "飞常准接口地址") + flag.BoolVar(&useFile, "use-file", true, "是否使用conf文件作为配置来源") + flag.StringVar(&confFile, "conf", "./conf.ini", "conf文件位置") + + flag.Parse() + + if useFile { + reloadConfig(confFile) + log.Logger.Printf("[%s]:%s%s", "CONF", "使用文件载入配置,文件位置:", confFile) + } else { + log.Logger.Printf("[%s]:%s", "CONF", "使用命令行参数载入配置") + } +} diff --git a/constant/version.go b/constant/version.go new file mode 100644 index 0000000..17a26f8 --- /dev/null +++ b/constant/version.go @@ -0,0 +1,6 @@ +package constant + +var ( + Version = "unknown version" + BuildTime = "unknown time" +) diff --git a/src/go.mod b/go.mod similarity index 54% rename from src/go.mod rename to go.mod index f4f3f1a..272033c 100644 --- a/src/go.mod +++ b/go.mod @@ -3,3 +3,8 @@ module dextercai.com/feeyo-adsb-golang go 1.13 // require github.com/Unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8 + +require ( + github.com/Unknwon/goconfig v1.0.0 + github.com/smartystreets/goconvey v1.7.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..be2f4c0 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/Unknwon/goconfig v1.0.0 h1:9IAu/BYbSLQi8puFjUQApZTxIHqSwrj5d8vpP8vTq4A= +github.com/Unknwon/goconfig v1.0.0/go.mod h1:wngxua9XCNjvHjDiTiV26DaKDT+0c63QR6H5hjVUUxw= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= +github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..f10d25e --- /dev/null +++ b/log/log.go @@ -0,0 +1,12 @@ +package log + +import ( + "log" + "os" +) + +var Logger *log.Logger + +func init() { + Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime) +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..7c14f19 --- /dev/null +++ b/makefile @@ -0,0 +1,136 @@ +NAME=feeyo-adsb-golang +BINDIR=bin +VERSION=$(shell git describe --tags || echo "unknown version") +BUILDTIME=$(shell date -u) +GOBUILD=CGO_ENABLED=0 go build -trimpath -ldflags '-X "dextercai.com/feeyo-adsb-golang/constant.Version=$(VERSION)" \ + -X "dextercai.com/feeyo-adsb-golang/constant.BuildTime=$(BUILDTIME)" \ + -w -s -buildid=' + +PLATFORM_LIST = \ + darwin-amd64 \ + darwin-amd64-v3 \ + darwin-arm64 \ + linux-386 \ + linux-amd64 \ + linux-amd64-v3 \ + linux-armv5 \ + linux-armv6 \ + linux-armv7 \ + linux-armv8 \ + linux-mips-softfloat \ + linux-mips-hardfloat \ + linux-mipsle-softfloat \ + linux-mipsle-hardfloat \ + linux-mips64 \ + linux-mips64le \ + freebsd-386 \ + freebsd-amd64 \ + freebsd-amd64-v3 \ + freebsd-arm64 + +WINDOWS_ARCH_LIST = \ + windows-386 \ + windows-amd64 \ + windows-amd64-v3 \ + windows-arm64 \ + windows-arm32v7 + +all: linux-amd64 darwin-amd64 windows-amd64 # Most used + +docker: + GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +darwin-amd64: + GOARCH=amd64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +darwin-amd64-v3: + GOARCH=amd64 GOOS=darwin GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +darwin-arm64: + GOARCH=arm64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-386: + GOARCH=386 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-amd64: + GOARCH=amd64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-amd64-v3: + GOARCH=amd64 GOOS=linux GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-armv5: + GOARCH=arm GOOS=linux GOARM=5 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-armv6: + GOARCH=arm GOOS=linux GOARM=6 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-armv7: + GOARCH=arm GOOS=linux GOARM=7 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-armv8: + GOARCH=arm64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-mips-softfloat: + GOARCH=mips GOMIPS=softfloat GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-mips-hardfloat: + GOARCH=mips GOMIPS=hardfloat GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-mipsle-softfloat: + GOARCH=mipsle GOMIPS=softfloat GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-mipsle-hardfloat: + GOARCH=mipsle GOMIPS=hardfloat GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-mips64: + GOARCH=mips64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +linux-mips64le: + GOARCH=mips64le GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +freebsd-386: + GOARCH=386 GOOS=freebsd $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +freebsd-amd64: + GOARCH=amd64 GOOS=freebsd $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +freebsd-amd64-v3: + GOARCH=amd64 GOOS=freebsd GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +freebsd-arm64: + GOARCH=arm64 GOOS=freebsd $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ + +windows-386: + GOARCH=386 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe + +windows-amd64: + GOARCH=amd64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe + +windows-amd64-v3: + GOARCH=amd64 GOOS=windows GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe + +windows-arm64: + GOARCH=arm64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe + +windows-arm32v7: + GOARCH=arm GOOS=windows GOARM=7 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe + +gz_releases=$(addsuffix .gz, $(PLATFORM_LIST)) +zip_releases=$(addsuffix .zip, $(WINDOWS_ARCH_LIST)) + +$(gz_releases): %.gz : % + chmod +x $(BINDIR)/$(NAME)-$(basename $@) + gzip -f -S -$(VERSION).gz $(BINDIR)/$(NAME)-$(basename $@) + +$(zip_releases): %.zip : % + zip -m -j $(BINDIR)/$(NAME)-$(basename $@)-$(VERSION).zip $(BINDIR)/$(NAME)-$(basename $@).exe + +all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST) + +releases: $(gz_releases) $(zip_releases) + +lint: + golangci-lint run ./... + +clean: + rm $(BINDIR)/* \ No newline at end of file diff --git a/src/adsb.go b/src/adsb.go deleted file mode 100644 index 79633f6..0000000 --- a/src/adsb.go +++ /dev/null @@ -1,116 +0,0 @@ -package main - -import ( - "bytes" - "compress/zlib" - "encoding/base64" - "fmt" - // "github.com/Unknwon/goconfig" - "io/ioutil" - "net" - "net/http" - "net/url" - "os" - "strings" - "time" - "flag" -) - -var ( - ipDump1090 string - portDump1090 string - feeyoUrl string - UUID string -) -// var Config *goconfig.ConfigFile - -var err error -func main() { - // initConfig() - - fmt.Println("本项目地址: https://github.com/dextercai/feeyo-adsb-golang") - fmt.Println("温馨提示: 二次分发时请遵守GPL3.0协议") - fmt.Println("=============================================================================================") - fmt.Println("敬告: 请不要尝试将相关电波数据传送至FR24, RadarBox, FA等境外平台, 这将严重违反无线电管理条例以及国家安全法!") - flag.StringVar(&UUID, "uuid", "", "UUID 16位") - flag.StringVar(&ipDump1090, "ip", "127.0.0.1", "设备IP") - flag.StringVar(&portDump1090, "port","30003", "Dump1090端口") - flag.StringVar(&feeyoUrl, "feeyoUrl", "https://adsb.feeyo.com/adsb/ReceiveCompressADSB.php", "飞常准接口地址") - flag.Parse() - - if UUID == "" || len(UUID) != 16 || ipDump1090 == "" || portDump1090 == "" || feeyoUrl == "" || err != nil { - println("配置错误") - os.Exit(0) - } - for { - dump1090Conn, err := net.Dial("tcp", ipDump1090+":"+portDump1090) - if err != nil { - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "\t连接到Dump1090失败\t", err.Error()) - time.Sleep(15 * time.Second) - continue - } else { - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "\t连接到Dump1090成功\t") - } - var buf [8192]byte - for { - read, err := dump1090Conn.Read(buf[0:]) - if err != nil { - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "\t读取数据错误\t", err.Error()) - _ = dump1090Conn.Close() - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "\t已断开连接,正尝试重连\t") - break - } else { - if buf[read-1] == 10 { - sendMessage(buf[0:read]) - } - } - } - } - -} -// func initConfig() { -// var err error -// Config, err = goconfig.LoadConfigFile("conf.ini") -// if err != nil { -// fmt.Println("conf.ini配置文件不存在,请检查.") -// os.Exit(0) -// } -// } - -func sendMessage(line []byte) { - sourceData := base64.StdEncoding.EncodeToString(DoZlibCompress(line)) - postValue := url.Values{} - postValue.Set("from", UUID) - postValue.Set("code", sourceData) - resp, err := http.Post(feeyoUrl, "application/x-www-form-urlencoded", strings.NewReader(postValue.Encode())) - if err != nil { - println(time.Now().Format("2006-01-02 15:04:05"), "\t上传错误\t", err.Error()) - return - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - println(time.Now().Format("2006-01-02 15:04:05"), "\t上传错误\t", err.Error()) - } else { - print("\r", time.Now().Format("2006-01-02 15:04:05"), "\t上传成功\t", string(body)) - } -} - -func DoZlibCompress(src []byte) []byte { - var in bytes.Buffer - w := zlib.NewWriter(&in) - _, _ = w.Write(src) - _ = w.Close() - return in.Bytes() -} - -/* -func DoZlibUnCompress(compressSrc []byte) []byte { - b := bytes.NewReader(compressSrc) - var out bytes.Buffer - r, _ := zlib.NewReader(b) - _, _ = io.Copy(&out, r) - return out.Bytes() -} - -*/ diff --git a/src/go.sum b/src/go.sum deleted file mode 100644 index e69de29..0000000 diff --git a/src/util/quickStrBytesConv.go b/util/quickStrBytesConv.go similarity index 100% rename from src/util/quickStrBytesConv.go rename to util/quickStrBytesConv.go