Skip to content

Commit

Permalink
支持自动下载 xraycore
Browse files Browse the repository at this point in the history
close #11
  • Loading branch information
Bpazy committed Aug 22, 2021
1 parent bb4c482 commit e32da03
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
11 changes: 1 addition & 10 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/Bpazy/xraysub/gen"
"github.com/Bpazy/xraysub/util"
"github.com/spf13/cobra"
"runtime"
)

var genCmd = &cobra.Command{
Expand All @@ -22,16 +21,8 @@ func init() {
util.CheckErr(genCmd.MarkFlagRequired(cUrl))
genCmd.Flags().StringVarP(&gen.Cfg.OutputFile, "output-file", "o", "./xray-config.json", "output configuration to file")
genCmd.Flags().BoolVarP(&gen.Cfg.DetectLatency, "detect-latency", "", true, "detect server's latency to choose the fastest node")
genCmd.Flags().StringVarP(&gen.Cfg.XrayCorePath, "xray", "", getDefaultXrayPath(), "xray-core path for detecting server's latency")
genCmd.Flags().StringVarP(&gen.Cfg.XrayCorePath, "xray", "", util.GetDefaultXrayPath(), "xray-core path for detecting server's latency")
genCmd.Flags().IntVarP(&gen.Cfg.XraySocksPort, "xray-socks-port", "", 1080, "xray-core listen socks port")
genCmd.Flags().IntVarP(&gen.Cfg.XrayHttpPort, "xray-http-port", "", 1081, "xray-core listen http port")
genCmd.Flags().IntVarP(&gen.Cfg.DetectThreadNumber, "detect-thread-number", "", 5, "detect server's latency threads number")
}

func getDefaultXrayPath() string {
defaultXrayPath := "./xray"
if runtime.GOOS == "windows" {
defaultXrayPath = defaultXrayPath + ".exe"
}
return defaultXrayPath
}
2 changes: 1 addition & 1 deletion cmd/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var downloadCmd = &cobra.Command{
Use: "download",
Short: "download xray-core",
Long: "download xray-core",
Run: xray.NewXrayDownloadCmdRun(),
RunE: xray.NewXrayDownloadCmdRun(),
}

func init() {
Expand Down
11 changes: 11 additions & 0 deletions util/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

import "runtime"

func GetDefaultXrayPath() string {
defaultXrayPath := "./xray"
if runtime.GOOS == "windows" {
defaultXrayPath = defaultXrayPath + ".exe"
}
return defaultXrayPath
}
61 changes: 47 additions & 14 deletions xray/xray.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xray

import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
Expand All @@ -13,6 +14,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -109,44 +111,75 @@ type GithubLatestRelease struct {
Reactions *Reactions `json:"reactions"`
}

func NewXrayDownloadCmdRun() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
func NewXrayDownloadCmdRun() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
downloadUrl, fileName, err := getDownloadUrl()
if err != nil {
util.CheckErr(err)
util.CheckErr(err)

fp, err := download(err, downloadUrl, fileName)
util.CheckErr(err)

fmt.Println("Unzipping files")
return unzip(err, fp)
}
}

func unzip(err error, fp string) error {
r, err := zip.OpenReader(fp)
if err != nil {
return fmt.Errorf("open zip error: %w", err)
}
defer r.Close()

var xf = new(zip.File)
for _, f := range r.File {
if strings.Contains(f.Name, "xray") {
xf = f
break
}
}
open, err := xf.Open()
if err != nil {
return fmt.Errorf("open zip file error: %w", err)
}
defer open.Close()

download(err, downloadUrl, fileName)
f, err := os.OpenFile(util.GetDefaultXrayPath(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("create xray-core file error: %w", err)
}
defer f.Close()
_, err = io.Copy(f, open)
if err != nil {
return fmt.Errorf("unzip: io copy error: %w", err)
}
return nil
}

func download(err error, downloadUrl string, fileName string) {
func download(err error, downloadUrl string, fileName string) (string, error) {
client := resty.New()
client.SetProxy("http://127.0.0.1:10809")
client.SetTimeout(30 * time.Second)
res, err := client.R().
SetDoNotParseResponse(true).
Get(downloadUrl)
res, err := client.R().SetDoNotParseResponse(true).Get(downloadUrl)
if err != nil {
util.CheckErr(fmt.Errorf("download error: %w", err))
return "", fmt.Errorf("download error: %w", err)
}
defer util.Closeq(res.RawResponse.Body)

outFile, err := os.Create(filepath.Clean(fileName))
if err != nil {
util.CheckErr(fmt.Errorf("create file error: %w", err))
return "", fmt.Errorf("create file error: %w", err)
}
defer util.Closeq(outFile)

bar := getDownloadProgressBar(res.RawResponse.ContentLength)
// io.Copy reads maximum 32kb size, it is perfect for large file download too
_, err = io.Copy(io.MultiWriter(outFile, bar), res.RawResponse.Body)
if err != nil {
util.CheckErr(fmt.Errorf("io copy error: %w", err))
return "", fmt.Errorf("io copy error: %w", err)
}

fmt.Println()
fmt.Printf("The xray-core is saved %s\n", fileName)
return outFile.Name(), nil
}

func getDownloadProgressBar(maxLength int64) *progressbar.ProgressBar {
Expand Down

0 comments on commit e32da03

Please sign in to comment.