2016-02-03 16:54:02 -05:00
// Package common contains common utilities that are shared among other packages.
// See each sub-package for detail.
package common
2016-03-09 18:33:01 -05:00
2020-08-27 04:37:46 -04:00
import (
"fmt"
"go/build"
"io/ioutil"
2021-04-06 15:08:03 -04:00
"net/http"
"net/url"
2020-08-27 04:37:46 -04:00
"os"
"path/filepath"
"strings"
2021-04-06 15:08:03 -04:00
"time"
2020-08-27 04:37:46 -04:00
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/errors"
2020-08-27 04:37:46 -04:00
)
2018-10-11 14:43:37 -04:00
2021-02-16 15:31:50 -05:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
2017-04-08 19:43:25 -04:00
2018-10-11 14:43:37 -04:00
var (
// ErrNoClue is for the situation that existing information is not enough to make a decision. For example, Router may return this error when there is no suitable route.
ErrNoClue = errors . New ( "not enough information for making a decision" )
)
2016-12-27 18:53:29 -05:00
// Must panics if err is not nil.
func Must ( err error ) {
if err != nil {
panic ( err )
}
}
2017-09-19 17:27:49 -04:00
2018-02-21 15:42:33 -05:00
// Must2 panics if the second parameter is not nil, otherwise returns the first parameter.
func Must2 ( v interface { } , err error ) interface { } {
Must ( err )
return v
2017-09-19 17:27:49 -04:00
}
2018-04-11 10:15:29 -04:00
// Error2 returns the err from the 2nd parameter.
func Error2 ( v interface { } , err error ) error {
return err
}
2020-08-27 04:37:46 -04:00
// envFile returns the name of the Go environment configuration file.
// Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166
func envFile ( ) ( string , error ) {
if file := os . Getenv ( "GOENV" ) ; file != "" {
if file == "off" {
return "" , fmt . Errorf ( "GOENV=off" )
}
return file , nil
}
dir , err := os . UserConfigDir ( )
if err != nil {
return "" , err
}
if dir == "" {
return "" , fmt . Errorf ( "missing user-config dir" )
}
return filepath . Join ( dir , "go" , "env" ) , nil
}
// GetRuntimeEnv returns the value of runtime environment variable,
// that is set by running following command: `go env -w key=value`.
func GetRuntimeEnv ( key string ) ( string , error ) {
file , err := envFile ( )
if err != nil {
return "" , err
}
if file == "" {
return "" , fmt . Errorf ( "missing runtime env file" )
}
var data [ ] byte
var runtimeEnv string
data , readErr := ioutil . ReadFile ( file )
if readErr != nil {
return "" , readErr
}
envStrings := strings . Split ( string ( data ) , "\n" )
for _ , envItem := range envStrings {
envItem = strings . TrimSuffix ( envItem , "\r" )
envKeyValue := strings . Split ( envItem , "=" )
if strings . EqualFold ( strings . TrimSpace ( envKeyValue [ 0 ] ) , key ) {
runtimeEnv = strings . TrimSpace ( envKeyValue [ 1 ] )
}
}
return runtimeEnv , nil
}
// GetGOBIN returns GOBIN environment variable as a string. It will NOT be empty.
func GetGOBIN ( ) string {
// The one set by user explicitly by `export GOBIN=/path` or `env GOBIN=/path command`
GOBIN := os . Getenv ( "GOBIN" )
if GOBIN == "" {
var err error
// The one set by user by running `go env -w GOBIN=/path`
GOBIN , err = GetRuntimeEnv ( "GOBIN" )
if err != nil {
// The default one that Golang uses
return filepath . Join ( build . Default . GOPATH , "bin" )
}
if GOBIN == "" {
return filepath . Join ( build . Default . GOPATH , "bin" )
}
return GOBIN
}
return GOBIN
}
// GetGOPATH returns GOPATH environment variable as a string. It will NOT be empty.
func GetGOPATH ( ) string {
// The one set by user explicitly by `export GOPATH=/path` or `env GOPATH=/path command`
GOPATH := os . Getenv ( "GOPATH" )
if GOPATH == "" {
var err error
// The one set by user by running `go env -w GOPATH=/path`
GOPATH , err = GetRuntimeEnv ( "GOPATH" )
if err != nil {
// The default one that Golang uses
return build . Default . GOPATH
}
if GOPATH == "" {
return build . Default . GOPATH
}
return GOPATH
}
return GOPATH
}
2021-04-06 15:08:03 -04:00
// FetchHTTPContent dials http(s) for remote content
func FetchHTTPContent ( target string ) ( [ ] byte , error ) {
parsedTarget , err := url . Parse ( target )
if err != nil {
return nil , newError ( "invalid URL: " , target ) . Base ( err )
}
if s := strings . ToLower ( parsedTarget . Scheme ) ; s != "http" && s != "https" {
return nil , newError ( "invalid scheme: " , parsedTarget . Scheme )
}
client := & http . Client {
Timeout : 30 * time . Second ,
}
resp , err := client . Do ( & http . Request {
Method : "GET" ,
URL : parsedTarget ,
Close : true ,
} )
if err != nil {
return nil , newError ( "failed to dial to " , target ) . Base ( err )
}
defer resp . Body . Close ( )
if resp . StatusCode != http . StatusOK {
return nil , newError ( "unexpected HTTP status code: " , resp . StatusCode )
}
content , err := ioutil . ReadAll ( resp . Body )
if err != nil {
return nil , newError ( "failed to read HTTP response" ) . Base ( err )
}
return content , nil
}