static expressvpn list

This commit is contained in:
Xavier Henner
2021-03-13 05:44:09 +00:00
parent c288f25dfe
commit 1f0de84b52
15 changed files with 447 additions and 176 deletions

View File

@@ -23,9 +23,11 @@ import (
)
type UConfig struct {
input string
config interface{}
hash string
cache map[string]interface{}
separator string
cacheLock sync.RWMutex
sync.RWMutex
}
@@ -136,14 +138,23 @@ func reduce(input interface{}) {
func New(input string, inline ...bool) (*UConfig, error) {
config := &UConfig{
config: nil,
input: input,
config: nil,
separator: ".",
}
return config, config.Load(input, inline...)
}
func (this *UConfig) Reload(inline ...bool) error {
return this.Load(this.input, inline...)
}
func (this *UConfig) SetSeparator(s string) {
this.separator = s
}
func (this *UConfig) Load(input string, inline ...bool) error {
this.Lock()
this.cache = map[string]interface{}{}
base, _ := os.Getwd()
content := fmt.Sprintf("/*base:%s*/\n", base)
if len(inline) > 0 && inline[0] {
@@ -182,36 +193,36 @@ func (this *UConfig) Load(input string, inline ...bool) error {
if arguments[0][0:1] != "/" {
arguments[0] = fmt.Sprintf("%s/%s", base, arguments[0])
}
base = ""
nbase := ""
if elements, err := filepath.Glob(arguments[0]); err == nil {
for _, element := range elements {
if mcontent, err := ioutil.ReadFile(element); err == nil {
base = filepath.Dir(element)
nbase = filepath.Dir(element)
expanded += string(mcontent)
}
}
}
if base != "" && strings.Index(expanded, "\n") >= 0 {
expanded = fmt.Sprintf("/*base:%s*/\n%s\n", base, expanded)
if nbase != "" && strings.Index(expanded, "\n") >= 0 {
expanded = fmt.Sprintf("/*base:%s*/\n%s\n/*base:%s*/\n", nbase, expanded, base)
}
case "|":
if arguments[0][0:1] != "/" {
arguments[0] = fmt.Sprintf("%s/%s", base, arguments[0])
}
base = ""
nbase := ""
if elements, err := filepath.Glob(arguments[0]); err == nil {
for _, element := range elements {
if element[0:1] != "/" {
element = fmt.Sprintf("%s/%s", base, element)
}
if mcontent, err := exec.Command(element, strings.Join(arguments[1:], " ")).Output(); err == nil {
base = filepath.Dir(element)
nbase = filepath.Dir(element)
expanded += string(mcontent)
}
}
}
if base != "" && strings.Index(expanded, "\n") >= 0 {
expanded = fmt.Sprintf("/*base:%s*/\n%s\n", base, expanded)
if nbase != "" && strings.Index(expanded, "\n") >= 0 {
expanded = fmt.Sprintf("/*base:%s*/\n%s\n/*base:%s*/\n", nbase, expanded, base)
}
case "@":
requester := http.Client{
@@ -227,16 +238,27 @@ func (this *UConfig) Load(input string, inline ...bool) error {
case "&":
expanded += os.Getenv(arguments[0])
case "!":
arguments[0] = strings.ToLower(arguments[0])
for index := 1; index < len(os.Args); index++ {
option := strings.ToLower(os.Args[index])
if option == "--"+arguments[0] {
if index == len(os.Args)-1 || strings.HasPrefix(os.Args[index+1], "--") {
expanded = "true"
} else {
expanded = os.Args[index+1]
if matcher := rcache.Get(fmt.Sprintf(`(?i)^--?(no-?)?(?:%s)(?:(=)(.+))?$`, arguments[0])); matcher != nil {
for index := 1; index < len(os.Args); index++ {
option := os.Args[index]
if option == "--" {
break
}
if captures := matcher.FindStringSubmatch(option); captures != nil {
if captures[2] == "=" {
expanded = captures[3]
} else {
if index == len(os.Args)-1 || strings.HasPrefix(os.Args[index+1], "-") {
expanded = "true"
if captures[1] != "" {
expanded = "false"
}
} else {
expanded = os.Args[index+1]
}
}
break
}
break
}
}
case "-":
@@ -258,15 +280,14 @@ func (this *UConfig) Load(input string, inline ...bool) error {
content = fmt.Sprintf("%s%s%s", content[0:indexes[0]], expanded, content[indexes[1]:len(content)])
}
this.hash = fmt.Sprintf("%x", sha1.Sum([]byte(content)))
if err := json.Unmarshal([]byte(content), &this.config); err != nil {
if err := json.Unmarshal([]byte("{"+content+"}"), &this.config); err != nil {
this.config = nil
var config interface{}
if err := json.Unmarshal([]byte(content), &config); err != nil {
if err := json.Unmarshal([]byte("{"+content+"}"), &config); err != nil {
if syntax, ok := err.(*json.SyntaxError); ok && syntax.Offset < int64(len(content)) {
if start := strings.LastIndex(content[:syntax.Offset], "\n") + 1; start >= 0 {
line := strings.Count(content[:start], "\n") + 1
this.Unlock()
return errors.New(fmt.Sprintf("%s at line %d near %s", syntax, line, content[start:syntax.Offset]))
return fmt.Errorf("uconfig: %s at line %d near %s", syntax, line, content[start:syntax.Offset])
}
}
this.Unlock()
@@ -274,6 +295,9 @@ func (this *UConfig) Load(input string, inline ...bool) error {
}
}
this.config = config
this.hash = fmt.Sprintf("%x", sha1.Sum([]byte(content)))
this.cache = map[string]interface{}{}
reduce(this.config)
this.Unlock()
return nil
@@ -320,8 +344,8 @@ func (this *UConfig) GetPaths(path string) []string {
}
this.cacheLock.RUnlock()
if path != "" {
prefix = "."
for _, part := range strings.Split(path, ".") {
prefix = this.separator
for _, part := range strings.Split(path, this.separator) {
kind := reflect.TypeOf(current).Kind()
index, err := strconv.Atoi(part)
if (kind == reflect.Slice && (err != nil || index < 0 || index >= len(current.([]interface{})))) || (kind != reflect.Slice && kind != reflect.Map) {
@@ -367,14 +391,14 @@ func (this *UConfig) value(path string) (string, error) {
this.RLock()
if current == nil || path == "" {
this.RUnlock()
return "", fmt.Errorf("invalid parameter")
return "", errors.New(`uconfig: invalid parameter`)
}
this.cacheLock.RLock()
if this.cache[path] != nil {
if current, ok := this.cache[path].(bool); ok && !current {
this.cacheLock.RUnlock()
this.RUnlock()
return "", fmt.Errorf("invalid path")
return "", errors.New(`uconfig: invalid path`)
}
if current, ok := this.cache[path].(string); ok {
this.cacheLock.RUnlock()
@@ -383,7 +407,7 @@ func (this *UConfig) value(path string) (string, error) {
}
}
this.cacheLock.RUnlock()
for _, part := range strings.Split(path, ".") {
for _, part := range strings.Split(path, this.separator) {
kind := reflect.TypeOf(current).Kind()
index, err := strconv.Atoi(part)
if (kind == reflect.Slice && (err != nil || index < 0 || index >= len(current.([]interface{})))) || (kind != reflect.Slice && kind != reflect.Map) {
@@ -391,7 +415,7 @@ func (this *UConfig) value(path string) (string, error) {
this.cache[path] = false
this.cacheLock.Unlock()
this.RUnlock()
return "", fmt.Errorf("invalid path")
return "", errors.New(`uconfig: invalid path`)
}
if kind == reflect.Slice {
current = current.([]interface{})[index]
@@ -401,7 +425,7 @@ func (this *UConfig) value(path string) (string, error) {
this.cache[path] = false
this.cacheLock.Unlock()
this.RUnlock()
return "", fmt.Errorf("invalid path")
return "", errors.New(`uconfig: invalid path`)
}
}
}
@@ -416,7 +440,7 @@ func (this *UConfig) value(path string) (string, error) {
this.cache[path] = false
this.cacheLock.Unlock()
this.RUnlock()
return "", fmt.Errorf("invalid path")
return "", errors.New(`uconfig: invalid path`)
}
func (this *UConfig) GetBoolean(path string, fallback bool) bool {
@@ -566,3 +590,28 @@ func (this *UConfig) GetDurationBounds(path string, fallback, min, max float64)
}
return math.Max(math.Min(nvalue, max), min)
}
func Duration(input float64) time.Duration {
return time.Duration(input * float64(time.Second))
}
func Args() (args []string) {
for index := 1; index < len(os.Args); index++ {
option := os.Args[index]
if args == nil {
if option[0] == '-' {
if option != "-" && option != "--" && strings.Index(option, "=") < 0 && index < len(os.Args)-1 && os.Args[index+1][0] != '-' {
index++
}
} else {
args = []string{}
}
}
if args != nil {
args = append(args, option)
} else if option == "--" {
args = []string{}
}
}
return
}

View File

@@ -25,12 +25,13 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
)
var zipData string
var zipData = map[string]string{}
// file holds unzipped read-only file contents and file metadata.
type file struct {
@@ -44,19 +45,40 @@ type statikFS struct {
dirs map[string][]string
}
const defaultNamespace = "default"
// IsDefaultNamespace returns true if the assetNamespace is
// the default one
func IsDefaultNamespace(assetNamespace string) bool {
return assetNamespace == defaultNamespace
}
// Register registers zip contents data, later used to initialize
// the statik file system.
func Register(data string) {
zipData = data
RegisterWithNamespace(defaultNamespace, data)
}
// New creates a new file system with the registered zip contents data.
// RegisterWithNamespace registers zip contents data and set asset namespace,
// later used to initialize the statik file system.
func RegisterWithNamespace(assetNamespace string, data string) {
zipData[assetNamespace] = data
}
// New creates a new file system with the default registered zip contents data.
// It unzips all files and stores them in an in-memory map.
func New() (http.FileSystem, error) {
if zipData == "" {
return NewWithNamespace(defaultNamespace)
}
// NewWithNamespace creates a new file system with the registered zip contents data.
// It unzips all files and stores them in an in-memory map.
func NewWithNamespace(assetNamespace string) (http.FileSystem, error) {
asset, ok := zipData[assetNamespace]
if !ok {
return nil, errors.New("statik/fs: no zip data registered")
}
zipReader, err := zip.NewReader(strings.NewReader(zipData), int64(len(zipData)))
zipReader, err := zip.NewReader(strings.NewReader(asset), int64(len(asset)))
if err != nil {
return nil, err
}
@@ -108,21 +130,12 @@ func (di dirInfo) ModTime() time.Time { return time.Time{} }
func (di dirInfo) IsDir() bool { return true }
func (di dirInfo) Sys() interface{} { return nil }
func unzip(zf *zip.File) ([]byte, error) {
rc, err := zf.Open()
if err != nil {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
}
// Open returns a file matching the given file name, or os.ErrNotExists if
// no file matching the given file name is found in the archive.
// If a directory is requested, Open returns the file named "index.html"
// in the requested directory, if that file exists.
func (fs *statikFS) Open(name string) (http.File, error) {
name = strings.Replace(name, "//", "/", -1)
name = filepath.ToSlash(filepath.Clean(name))
if f, ok := fs.files[name]; ok {
return newHTTPFile(f), nil
}
@@ -182,7 +195,7 @@ func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
}
// If count is positive, the specified number of files will be returned,
// and if negative, all remaining files will be returned.
// and if non-positive, all remaining files will be returned.
// The reading position of which file is returned is held in dirIndex.
fnames := f.file.fs.dirs[di.name]
flen := len(fnames)
@@ -196,7 +209,7 @@ func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
return fis, io.EOF
}
var end int
if count < 0 {
if count <= 0 {
end = flen
} else {
end = start + count
@@ -214,3 +227,12 @@ func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
func (f *httpFile) Close() error {
return nil
}
func unzip(zf *zip.File) ([]byte, error) {
rc, err := zf.Open()
if err != nil {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
}

46
vendor/modules.txt vendored
View File

@@ -1,5 +1,47 @@
# github.com/pyke369/golang-support v0.0.0-20190703174728-34ca97aa79e9
# 9fans.net/go v0.0.2
## explicit
# github.com/alecthomas/gometalinter v3.0.0+incompatible
## explicit
# github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15
## explicit
# github.com/fatih/gomodifytags v1.13.0
## explicit
# github.com/fatih/motion v1.1.0
## explicit
# github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
## explicit
# github.com/josharian/impl v1.0.0
## explicit
# github.com/jstemmer/gotags v1.4.1
## explicit
# github.com/kisielk/errcheck v1.6.0
## explicit
# github.com/klauspost/asmfmt v1.2.3
## explicit
# github.com/nicksnyder/go-i18n v1.10.1
## explicit
# github.com/nsf/gocode v0.0.0-20190302080247-5bee97b48836
## explicit
# github.com/pelletier/go-toml v1.8.1
## explicit
# github.com/pyke369/golang-support v0.0.0-20210309170400-e309156a86e0
## explicit
github.com/pyke369/golang-support/rcache
github.com/pyke369/golang-support/uconfig
# github.com/rakyll/statik v0.1.6
# github.com/rakyll/statik v0.1.7
## explicit
github.com/rakyll/statik/fs
# github.com/rogpeppe/godef v1.1.2
## explicit
# github.com/zmb3/gogetdoc v0.0.0-20190228002656-b37376c5da6a
## explicit
# golang.org/x/mod v0.4.2
## explicit
# golang.org/x/sys v0.0.0-20210309074719-68d13333faf2
## explicit
# golang.org/x/tools v0.1.0
## explicit
# gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780
## explicit
# gopkg.in/yaml.v2 v2.4.0
## explicit