mirror of https://github.com/v2ray/v2ray-core
parent
acdada81ed
commit
0de60adf3c
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func copyConfigFile(src, dest string, goOS GoOS) error {
|
||||
content, err := ioutil.ReadFile(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
str := string(content)
|
||||
str = strings.Replace(str, "\r\n", "\n", -1)
|
||||
if goOS == Windows {
|
||||
str = strings.Replace(str, "\n", "\r\n", -1)
|
||||
}
|
||||
return ioutil.WriteFile(dest, []byte(str), 0777)
|
||||
}
|
||||
|
||||
func copyConfigFiles(dir string, goOS GoOS) error {
|
||||
GOPATH := os.Getenv("GOPATH")
|
||||
srcDir := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "release", "config")
|
||||
src := filepath.Join(srcDir, "vpoint_socks_vmess.json")
|
||||
dest := filepath.Join(dir, "vpoint_socks_vmess.json")
|
||||
if goOS == Windows || goOS == MacOS {
|
||||
dest = filepath.Join(dir, "config.json")
|
||||
}
|
||||
err := copyConfigFile(src, dest, goOS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if goOS == Windows || goOS == MacOS {
|
||||
return nil
|
||||
}
|
||||
|
||||
src = filepath.Join(srcDir, "vpoint_vmess_freedom.json")
|
||||
dest = filepath.Join(dir, "vpoint_vmess_freedom.json")
|
||||
return copyConfigFile(src, dest, goOS)
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GoOS string
|
||||
|
||||
const (
|
||||
Windows = GoOS("windows")
|
||||
MacOS = GoOS("darwin")
|
||||
Linux = GoOS("linux")
|
||||
UnknownOS = GoOS("unknown")
|
||||
)
|
||||
|
||||
type GoArch string
|
||||
|
||||
const (
|
||||
X86 = GoArch("386")
|
||||
Amd64 = GoArch("amd64")
|
||||
Arm = GoArch("arm")
|
||||
Arm64 = GoArch("arm64")
|
||||
UnknownArch = GoArch("unknown")
|
||||
)
|
||||
|
||||
func parseOS(rawOS string) GoOS {
|
||||
osStr := strings.ToLower(rawOS)
|
||||
if osStr == "windows" || osStr == "win" {
|
||||
return Windows
|
||||
}
|
||||
if osStr == "darwin" || osStr == "mac" || osStr == "macos" || osStr == "osx" {
|
||||
return MacOS
|
||||
}
|
||||
if osStr == "linux" || osStr == "debian" || osStr == "ubuntu" || osStr == "redhat" || osStr == "centos" {
|
||||
return Linux
|
||||
}
|
||||
return UnknownOS
|
||||
}
|
||||
|
||||
func parseArch(rawArch string) GoArch {
|
||||
archStr := strings.ToLower(rawArch)
|
||||
if archStr == "x86" || archStr == "386" || archStr == "i386" {
|
||||
return X86
|
||||
}
|
||||
if archStr == "amd64" || archStr == "x86-64" || archStr == "x64" {
|
||||
return Amd64
|
||||
}
|
||||
if archStr == "arm" {
|
||||
return Arm
|
||||
}
|
||||
if archStr == "arm64" {
|
||||
return Arm64
|
||||
}
|
||||
return UnknownArch
|
||||
}
|
||||
|
||||
func getSuffix(os GoOS, arch GoArch) string {
|
||||
suffix := "-custom"
|
||||
switch os {
|
||||
case Windows:
|
||||
switch arch {
|
||||
case X86:
|
||||
suffix = "-windows-32"
|
||||
case Amd64:
|
||||
suffix = "-windows-64"
|
||||
}
|
||||
case MacOS:
|
||||
suffix = "-macos"
|
||||
case Linux:
|
||||
switch arch {
|
||||
case X86:
|
||||
suffix = "-linux-32"
|
||||
case Amd64:
|
||||
suffix = "-linux-64"
|
||||
case Arm:
|
||||
suffix = "-linux-arm"
|
||||
case Arm64:
|
||||
suffix = "-linux-arm64"
|
||||
}
|
||||
|
||||
}
|
||||
return suffix
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
|
||||
func TestParseOS(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
assert.Pointer(parseOS("windows")).Equals(Windows)
|
||||
assert.Pointer(parseOS("macos")).Equals(MacOS)
|
||||
assert.Pointer(parseOS("linux")).Equals(Linux)
|
||||
assert.Pointer(parseOS("test")).Equals(UnknownOS)
|
||||
}
|
||||
|
||||
func TestParseArch(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
assert.Pointer(parseArch("x86")).Equals(X86)
|
||||
assert.Pointer(parseArch("x64")).Equals(Amd64)
|
||||
assert.Pointer(parseArch("arm")).Equals(Arm)
|
||||
assert.Pointer(parseArch("arm64")).Equals(Arm64)
|
||||
assert.Pointer(parseArch("test")).Equals(UnknownArch)
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func buildV2Ray(targetFile string, version string, goOS GoOS, goArch GoArch) error {
|
||||
ldFlags := "-s"
|
||||
if version != "" {
|
||||
year, month, day := time.Now().UTC().Date()
|
||||
today := fmt.Sprintf("%04d%02d%02d", year, int(month), day)
|
||||
ldFlags = ldFlags + " -X github.com/v2ray/v2ray-core.version=" + version + " -X github.com/v2ray/v2ray-core.build=" + today
|
||||
}
|
||||
cmd := exec.Command("go", "build", "-o", targetFile, "-compiler", "gc", "-ldflags", ldFlags, "github.com/v2ray/v2ray-core/release/server")
|
||||
cmd.Env = append(cmd.Env, "GOOS="+string(goOS), "GOARCH="+string(goArch))
|
||||
cmd.Env = append(cmd.Env, os.Environ()...)
|
||||
_, err := cmd.Output()
|
||||
return err
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
|
||||
func TestBuildAndRun(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
gopath := os.Getenv("GOPATH")
|
||||
target := filepath.Join(gopath, "src", "v2ray_test")
|
||||
fmt.Println(target)
|
||||
goOS := parseOS(runtime.GOOS)
|
||||
goArch := parseArch(runtime.GOARCH)
|
||||
err := buildV2Ray(target, "v1.0", goOS, goArch)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
outBuffer := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
errBuffer := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
configFile := filepath.Join(gopath, "src", "github.com", "v2ray", "v2ray-core", "release", "config", "vpoint_socks_vmess.json")
|
||||
cmd := exec.Command(target, "--config="+configFile)
|
||||
cmd.Stdout = outBuffer
|
||||
cmd.Stderr = errBuffer
|
||||
cmd.Start()
|
||||
|
||||
<-time.After(1 * time.Second)
|
||||
cmd.Process.Kill()
|
||||
|
||||
outStr := string(outBuffer.Bytes())
|
||||
errStr := string(errBuffer.Bytes())
|
||||
|
||||
assert.Bool(strings.Contains(outStr, "v1.0")).IsTrue()
|
||||
assert.Int(len(errStr)).Equals(0)
|
||||
|
||||
os.Remove(target)
|
||||
}
|
Loading…
Reference in new issue