diff --git a/.travis.yml b/.travis.yml index 0277c38f..cb545b2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,12 +36,13 @@ install: - go get github.com/mitchellh/gox - gem install fpm -v 1.13.1 script: - - cd $P_DIR/service && CGO_ENABLED=0 gox -ldflags '-extldflags "-static"' -osarch="linux/386 linux/amd64 linux/arm linux/arm64" -ldflags="-X github.com/v2rayA/v2rayA/global.Version=${VERSION:1} -s -w" -output="$P_DIR/v2raya_{{.OS}}_{{.Arch}}_$VERSION" - - mv $P_DIR/v2raya_linux_386_$VERSION $P_DIR/v2raya_linux_x86_$VERSION && mv $P_DIR/v2raya_linux_amd64_$VERSION $P_DIR/v2raya_linux_x64_$VERSION - yarn --cwd $P_DIR/gui && yarn --cwd $P_DIR/gui build - cd $P_DIR - tar -zcvf web.tar.gz web/ - - files="web=/etc/v2raya install/universal/v2raya.service=/etc/systemd/system/v2raya.service install/universal/v2raya.png=/usr/share/icons/hicolor/512x512/apps/v2raya.png install/universal/v2raya.desktop=/usr/share/applications/v2raya.desktop" + - cp -r web $P_DIR/service/server/router/ + - cd $P_DIR/service && CGO_ENABLED=0 gox -ldflags '-extldflags "-static"' -osarch="linux/386 linux/amd64 linux/arm linux/arm64" -ldflags="-X github.com/v2rayA/v2rayA/global.Version=${VERSION:1} -s -w" -output="$P_DIR/v2raya_{{.OS}}_{{.Arch}}_$VERSION" + - mv $P_DIR/v2raya_linux_386_$VERSION $P_DIR/v2raya_linux_x86_$VERSION && mv $P_DIR/v2raya_linux_amd64_$VERSION $P_DIR/v2raya_linux_x64_$VERSION + - files="install/universal/v2raya.service=/etc/systemd/system/v2raya.service install/universal/v2raya.png=/usr/share/icons/hicolor/512x512/apps/v2raya.png install/universal/v2raya.desktop=/usr/share/applications/v2raya.desktop" - params="--verbose -f -n $NAME -v ${VERSION:1} --url https://github.com/v2rayA/v2raya --maintainer v2rayA --after-install install/universal/after_install.sh --after-upgrade $P_DIR/install/universal/after_upgrade.sh --before-remove $P_DIR/install/universal/before_remove.sh --after-remove $P_DIR/install/universal/after_remove.sh" - fpm -s dir -t deb -p installer_debian_x86_$VERSION.deb --description "$DESC" $params --architecture i386 v2raya_linux_x86_$VERSION=/usr/bin/v2raya $files - fpm -s dir -t deb -p installer_debian_x64_$VERSION.deb --description "$DESC" $params --architecture=amd64 v2raya_linux_x64_$VERSION=/usr/bin/v2raya $files diff --git a/install/aur/deploy_v2raya_bin.sh b/install/aur/deploy_v2raya_bin.sh index d15c046a..6c4c6ccd 100644 --- a/install/aur/deploy_v2raya_bin.sh +++ b/install/aur/deploy_v2raya_bin.sh @@ -7,7 +7,6 @@ sha1sums_aarch64=$(sha1sum "$P_DIR"/v2raya_linux_arm64_$VERSION | awk '{print $1 sha_service=$(sha1sum "$P_DIR"/install/universal/v2raya.service | awk '{print $1}') sha_png=$(sha1sum "$P_DIR"/install/universal/v2raya.png | awk '{print $1}') sha_desktop=$(sha1sum "$P_DIR"/install/universal/v2raya.desktop | awk '{print $1}') -sha_web=$(sha1sum "$P_DIR"/web.tar.gz | awk '{print $1}') mkdir -p /tmp/prepare/v2raya-bin cd /tmp/prepare/v2raya-bin @@ -26,7 +25,6 @@ sed -i s/{{sha1sums_aarch64}}/"${sha1sums_aarch64}"/g PKGBUILD .SRCINFO sed -i s/{{sha_service}}/"${sha_service}"/g PKGBUILD .SRCINFO sed -i s/{{sha_png}}/"${sha_png}"/g PKGBUILD .SRCINFO sed -i s/{{sha_desktop}}/"${sha_desktop}"/g PKGBUILD .SRCINFO -sed -i s/{{sha_web}}/"${sha_web}"/g PKGBUILD .SRCINFO cd /tmp/ git clone ssh://aur@aur.archlinux.org/v2raya-bin.git diff --git a/install/aur/v2raya-bin/.SRCINFO b/install/aur/v2raya-bin/.SRCINFO index a34e6323..4a4bfd48 100644 --- a/install/aur/v2raya-bin/.SRCINFO +++ b/install/aur/v2raya-bin/.SRCINFO @@ -18,11 +18,9 @@ pkgbase = v2raya-bin source = v2raya.service source = v2raya.png source = v2raya.desktop - source = web_v{{pkgver}}.tar.gz::https://apt.v2raya.mzz.pub/pool/main/v/v2raya/web_v{{pkgver}}.tar.gz sha1sums = {{sha_service}} sha1sums = {{sha_png}} sha1sums = {{sha_desktop}} - sha1sums = {{sha_web}} source_i686 = v2raya::https://apt.v2raya.mzz.pub/pool/main/v/v2raya/v2raya_linux_x86_v{{pkgver}} sha1sums_i686 = {{sha1sums_i686}} source_x86_64 = v2raya::https://apt.v2raya.mzz.pub/pool/main/v/v2raya/v2raya_linux_x64_v{{pkgver}} diff --git a/install/aur/v2raya-bin/PKGBUILD b/install/aur/v2raya-bin/PKGBUILD index b65c6325..88079d9f 100644 --- a/install/aur/v2raya-bin/PKGBUILD +++ b/install/aur/v2raya-bin/PKGBUILD @@ -17,13 +17,11 @@ conflicts=('v2raya') sha_service={{sha_service}} sha_png={{sha_png}} sha_desktop={{sha_desktop}} -sha_web={{sha_web}} source=( "v2raya.service" "v2raya.png" "v2raya.desktop" - "web_v${pkgver}.tar.gz::https://apt.v2raya.mzz.pub/pool/main/v/v2raya/web_v${pkgver}.tar.gz" ) source_i686=( @@ -46,7 +44,6 @@ sha1sums=( "$sha_service" "$sha_png" "$sha_desktop" - "$sha_web" ) sha1sums_i686=( @@ -68,8 +65,6 @@ sha1sums_armv7h=( package() { cd "$srcdir" install -Dm 755 v2raya_v"${pkgver}" "${pkgdir}"/usr/bin/v2raya - find web -type d -exec install -vd "${pkgdir}"/etc/v2raya/{} \; - find web -type f -exec install -vm 644 {} "${pkgdir}"/etc/v2raya/{} \; install -Dm 644 v2raya.desktop -t "${pkgdir}"/usr/share/applications/ install -Dm 644 v2raya.service -t "${pkgdir}"/usr/lib/systemd/system/ install -Dm 644 v2raya.png "${pkgdir}"/usr/share/icons/hicolor/512x512/apps/v2raya.png diff --git a/install/aur/v2raya/PKGBUILD b/install/aur/v2raya/PKGBUILD index 6635df50..ba36a406 100644 --- a/install/aur/v2raya/PKGBUILD +++ b/install/aur/v2raya/PKGBUILD @@ -30,8 +30,6 @@ build() { package() { cd "${srcdir}"/"v2rayA-${pkgver}"/ install -Dm 755 service/v2raya -t "${pkgdir}"/usr/bin/ - find web -type d -exec install -vd "${pkgdir}"/etc/v2raya/{} \; - find web -type f -exec install -vm 644 {} "${pkgdir}"/etc/v2raya/{} \; install -dm 750 "${pkgdir}"/etc/v2raya/ install -Dm 644 install/universal/v2raya.desktop -t "${pkgdir}"/usr/share/applications/ install -Dm 644 install/universal/v2raya.service -t "${pkgdir}"/usr/lib/systemd/system/ diff --git a/service/.gitignore b/service/.gitignore index 546a311c..ed9beb79 100644 --- a/service/.gitignore +++ b/service/.gitignore @@ -8,4 +8,5 @@ /gin-bin /config.json /vendor -/main \ No newline at end of file +/main +/server/router/web \ No newline at end of file diff --git a/service/global/environmentConfig.go b/service/global/environmentConfig.go index 24e4c344..1d450d87 100644 --- a/service/global/environmentConfig.go +++ b/service/global/environmentConfig.go @@ -15,7 +15,7 @@ type Params struct { Config string `id:"config" short:"c" default:"/etc/v2raya" desc:"v2rayA configuration directory"` V2rayBin string `id:"v2ray-bin" desc:"Executable v2ray binary path. Auto-detect if put it empty."` V2rayConfigDirectory string `id:"v2ray-confdir" desc:"Additional v2ray config directory, files in it will be combined with config generated by v2rayA"` - WebDir string `id:"webdir" default:"/etc/v2raya/web" desc:"v2rayA web files directory"` + WebDir string `id:"webdir" desc:"v2rayA web files directory. use embedded files if not specify."` Mode string `id:"mode" short:"m" desc:"(deprecated) Options: systemctl, service, universal. Auto-detect if not set"` PluginListenPort int `short:"s" default:"32346" desc:"ssr, pingTunnel, etc."` ForceIPV6On bool `id:"force_ipv6_on" desc:"force open ipv6"` diff --git a/service/server/router/index.go b/service/server/router/index.go index 92d13bfa..93cfb1c8 100644 --- a/service/server/router/index.go +++ b/service/server/router/index.go @@ -1,6 +1,7 @@ package router import ( + "embed" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/gookit/color" @@ -9,6 +10,8 @@ import ( "github.com/v2rayA/v2rayA/db/configure" "github.com/v2rayA/v2rayA/global" "github.com/v2rayA/v2rayA/server/controller" + "io" + "io/fs" "log" "net" "net/http" @@ -17,32 +20,85 @@ import ( "path/filepath" ) +//go:embed web +var webRoot embed.FS + +// relativeFS implements fs.FS +type relativeFS struct { + root fs.FS + relativeDir string +} + +func (c relativeFS) Open(name string) (fs.File, error) { + return c.root.Open(path.Join(c.relativeDir, name)) +} + func ServeGUI(engine *gin.Engine) { - defer func() { - if msg := recover(); msg != nil { - log.Println("[ERROR]", msg) - } - }() + //defer func() { + // if msg := recover(); msg != nil { + // log.Println("[ERROR]", msg) + // } + //}() webDir := global.GetEnvironmentConfig().WebDir - if _, err := os.Stat(webDir); os.IsNotExist(err) { - log.Printf("[Warning] web files cannot be found at %v. web UI cannot be served", webDir) - } else { - filepath.Walk(webDir, func(path string, info os.FileInfo, err error) error { - if path == webDir { + if webDir == "" { + webFS := relativeFS{ + root: webRoot, + relativeDir: "web", + } + fs.WalkDir(webFS, "/", func(path string, info fs.DirEntry, err error) error { + if path == "/" { return nil } if info.IsDir() { - engine.Static("/"+info.Name(), path) + engine.StaticFS("/"+info.Name(), http.FS(relativeFS{ + root: webFS, + relativeDir: path, + })) return filepath.SkipDir } - engine.StaticFile("/"+info.Name(), path) + engine.GET("/"+info.Name(), func(ctx *gin.Context) { + ctx.FileFromFS(path, http.FS(webFS)) + }) return nil }) - engine.LoadHTMLFiles(path.Join(webDir, "index.html")) - engine.GET("/", func(context *gin.Context) { - context.HTML(http.StatusOK, "index.html", nil) + //engine.LoadHTMLFiles(path.Join(webDir, "index.html")) + engine.GET("/", func(ctx *gin.Context) { + f, err := webFS.Open("index.html") + if err != nil { + ctx.Status(400) + return + } + defer f.Close() + b, err := io.ReadAll(f) + if err != nil { + ctx.Status(400) + return + } + ctx.Header("Content-Type", "text/html; charset=utf-8") + ctx.String(http.StatusOK, string(b)) }) } + if webDir != "" { + if _, err := os.Stat(webDir); os.IsNotExist(err) { + log.Printf("[Warning] web files cannot be found at %v. web UI cannot be served", webDir) + } else { + filepath.Walk(webDir, func(path string, info os.FileInfo, err error) error { + if path == webDir { + return nil + } + if info.IsDir() { + engine.Static("/"+info.Name(), path) + return filepath.SkipDir + } + engine.StaticFile("/"+info.Name(), path) + return nil + }) + engine.LoadHTMLFiles(path.Join(webDir, "index.html")) + engine.GET("/", func(context *gin.Context) { + context.HTML(http.StatusOK, "index.html", nil) + }) + } + } app := global.GetEnvironmentConfig()