v2ray-core/testing/scenarios/server_env.go

77 lines
1.9 KiB
Go
Raw Normal View History

2015-12-04 15:49:10 +00:00
package scenarios
import (
2015-12-04 15:49:45 +00:00
"os"
"path/filepath"
2015-12-04 15:49:10 +00:00
_ "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/shell/point"
// The following are neccesary as they register handlers in their init functions.
_ "github.com/v2ray/v2ray-core/proxy/blackhole"
_ "github.com/v2ray/v2ray-core/proxy/dokodemo"
_ "github.com/v2ray/v2ray-core/proxy/freedom"
2016-01-24 20:48:38 +00:00
_ "github.com/v2ray/v2ray-core/proxy/http"
2016-01-28 12:40:00 +00:00
_ "github.com/v2ray/v2ray-core/proxy/shadowsocks"
2015-12-04 15:49:10 +00:00
_ "github.com/v2ray/v2ray-core/proxy/socks"
2015-12-07 19:32:38 +00:00
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
2015-12-04 15:49:10 +00:00
)
2015-12-04 16:32:42 +00:00
var (
2016-01-03 23:33:25 +00:00
runningServers = make([]*point.Point, 0, 10)
2015-12-04 16:32:42 +00:00
)
2015-12-04 15:49:10 +00:00
func TestFile(filename string) string {
2015-12-04 15:49:45 +00:00
return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename)
2015-12-04 15:49:10 +00:00
}
2015-12-04 16:32:42 +00:00
func InitializeServerSetOnce(testcase string) error {
2016-01-28 12:40:00 +00:00
if err := InitializeServerServer(testcase); err != nil {
2015-12-04 16:32:42 +00:00
return err
}
2016-01-28 12:40:00 +00:00
if err := InitializeServerClient(testcase); err != nil {
2015-12-04 16:32:42 +00:00
return err
}
return nil
}
2016-01-28 12:40:00 +00:00
func InitializeServerServer(testcase string) error {
return InitializeServer(TestFile(testcase + "_server.json"))
}
func InitializeServerClient(testcase string) error {
return InitializeServer(TestFile(testcase + "_client.json"))
}
2015-12-04 15:49:10 +00:00
func InitializeServer(configFile string) error {
2016-01-17 20:43:10 +00:00
config, err := point.LoadConfig(configFile)
2015-12-04 15:49:10 +00:00
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Failed to read config file (", configFile, "): ", configFile, err)
2015-12-04 15:49:10 +00:00
return err
}
vPoint, err := point.NewPoint(config)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Failed to create Point server: ", err)
2015-12-04 15:49:10 +00:00
return err
}
err = vPoint.Start()
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Error starting Point server: ", err)
2015-12-04 15:49:10 +00:00
return err
}
2016-01-03 23:33:25 +00:00
runningServers = append(runningServers, vPoint)
2015-12-04 15:49:10 +00:00
return nil
}
2016-01-03 23:33:25 +00:00
func CloseAllServers() {
for _, server := range runningServers {
server.Close()
}
runningServers = make([]*point.Point, 0, 10)
}