mirror of https://github.com/dnomd343/XProxy.git
dnomd343
2 years ago
8 changed files with 246 additions and 224 deletions
@ -1,94 +1,89 @@ |
|||||
package common |
package common |
||||
|
|
||||
import ( |
import ( |
||||
log "github.com/sirupsen/logrus" |
log "github.com/sirupsen/logrus" |
||||
"io" |
"io" |
||||
"io/ioutil" |
"io/ioutil" |
||||
"net/http" |
"net/http" |
||||
"os" |
"os" |
||||
"strings" |
"strings" |
||||
) |
) |
||||
|
|
||||
func CreateFolder(folderPath string) { |
func CreateFolder(folderPath string) { |
||||
log.Debugf("Create folder -> %s", folderPath) |
log.Debugf("Create folder -> %s", folderPath) |
||||
err := os.MkdirAll(folderPath, 0755) |
if err := os.MkdirAll(folderPath, 0755); err != nil { |
||||
if err != nil { |
log.Panicf("Failed to create folder -> %s", folderPath) |
||||
log.Panicf("Failed to create folder -> %s", folderPath) |
} |
||||
} |
|
||||
} |
} |
||||
|
|
||||
func IsFileExist(filePath string) bool { |
func IsFileExist(filePath string) bool { |
||||
s, err := os.Stat(filePath) |
s, err := os.Stat(filePath) |
||||
if err != nil { // file or folder not exist
|
if err != nil { // file or folder not exist
|
||||
return false |
return false |
||||
} |
} |
||||
return !s.IsDir() |
return !s.IsDir() |
||||
} |
} |
||||
|
|
||||
func WriteFile(filePath string, content string, overwrite bool) { |
func WriteFile(filePath string, content string, overwrite bool) { |
||||
if !overwrite && IsFileExist(filePath) { // file exist and don't overwrite
|
if !overwrite && IsFileExist(filePath) { // file exist and don't overwrite
|
||||
log.Debugf("File `%s` exist -> skip write", filePath) |
log.Debugf("File `%s` exist -> skip write", filePath) |
||||
return |
return |
||||
} |
} |
||||
log.Debugf("Write file `%s` -> \n%s", filePath, content) |
log.Debugf("Write file `%s` -> \n%s", filePath, content) |
||||
err := os.WriteFile(filePath, []byte(content), 0644) |
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil { |
||||
if err != nil { |
log.Panicf("Failed to write `%s` -> %v", filePath, err) |
||||
log.Panicf("Failed to write `%s` -> %v", filePath, err) |
} |
||||
} |
|
||||
} |
} |
||||
|
|
||||
func ListFiles(folderPath string, suffix string) []string { |
func ListFiles(folderPath string, suffix string) []string { |
||||
var fileList []string |
var fileList []string |
||||
files, err := ioutil.ReadDir(folderPath) |
files, err := ioutil.ReadDir(folderPath) |
||||
if err != nil { |
if err != nil { |
||||
log.Panicf("Failed to list folder -> %s", folderPath) |
log.Panicf("Failed to list folder -> %s", folderPath) |
||||
} |
} |
||||
for _, file := range files { |
for _, file := range files { |
||||
if strings.HasSuffix(file.Name(), suffix) { |
if strings.HasSuffix(file.Name(), suffix) { |
||||
fileList = append(fileList, file.Name()) |
fileList = append(fileList, file.Name()) |
||||
} |
} |
||||
} |
} |
||||
return fileList |
return fileList |
||||
} |
} |
||||
|
|
||||
func CopyFile(source string, target string) { |
func CopyFile(source string, target string) { |
||||
log.Infof("Copy file `%s` => `%s`", source, target) |
log.Infof("Copy file `%s` => `%s`", source, target) |
||||
if IsFileExist(target) { |
if IsFileExist(target) { |
||||
log.Warningf("File `%s` will be overrided", target) |
log.Debugf("File `%s` will be overrided", target) |
||||
} |
} |
||||
srcFile, err := os.Open(source) |
srcFile, err := os.Open(source) |
||||
if err != nil { |
defer srcFile.Close() |
||||
log.Panicf("Failed to open file -> %s", source) |
if err != nil { |
||||
} |
log.Panicf("Failed to open file -> %s", source) |
||||
dstFile, err := os.OpenFile(target, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
} |
||||
if err != nil { |
dstFile, err := os.OpenFile(target, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
||||
_ = srcFile.Close() |
defer dstFile.Close() |
||||
log.Panicf("Failed to open file -> %s", target) |
if err != nil { |
||||
} |
log.Panicf("Failed to open file -> %s", target) |
||||
_, err = io.Copy(dstFile, srcFile) |
} |
||||
_ = srcFile.Close() |
if _, err = io.Copy(dstFile, srcFile); err != nil { |
||||
_ = dstFile.Close() |
log.Panicf("Failed to copy from `%s` to `%s`", source, target) |
||||
if err != nil { |
} |
||||
log.Panicf("Failed to copy from `%s` to `%s`", source, target) |
|
||||
} |
|
||||
} |
} |
||||
|
|
||||
func DownloadFile(url string, file string) { |
func DownloadFile(url string, file string) { |
||||
log.Debugf("File download `%s` => `%s`", url, file) |
log.Debugf("File download `%s` => `%s`", url, file) |
||||
resp, err := http.Get(url) |
resp, err := http.Get(url) |
||||
if err != nil { |
defer resp.Body.Close() |
||||
log.Errorf("Download `%s` error -> %v", url, err) |
if err != nil { |
||||
return |
log.Errorf("Download `%s` error -> %v", url, err) |
||||
} |
return |
||||
out, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
} |
||||
if err != nil { |
output, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
||||
_ = resp.Body.Close() |
defer output.Close() |
||||
log.Panicf("Open `%s` error -> %v", file, err) |
if err != nil { |
||||
} |
log.Panicf("Open `%s` error -> %v", file, err) |
||||
_, err = io.Copy(out, resp.Body) |
} |
||||
_ = resp.Body.Close() |
if _, err = io.Copy(output, resp.Body); err != nil { |
||||
if err != nil { |
log.Panicf("File `%s` save error -> %v", file, err) |
||||
log.Panicf("File `%s` save error -> %v", file, err) |
} |
||||
} |
log.Infof("Download success `%s` => `%s`", url, file) |
||||
log.Infof("Download success `%s` => `%s`", url, file) |
|
||||
} |
} |
||||
|
@ -0,0 +1,43 @@ |
|||||
|
package common |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
log "github.com/sirupsen/logrus" |
||||
|
"net" |
||||
|
"os/exec" |
||||
|
"strings" |
||||
|
"syscall" |
||||
|
) |
||||
|
|
||||
|
func isIP(ipAddr string, isCidr bool) bool { |
||||
|
if !isCidr { |
||||
|
return net.ParseIP(ipAddr) != nil |
||||
|
} |
||||
|
_, _, err := net.ParseCIDR(ipAddr) |
||||
|
return err == nil |
||||
|
} |
||||
|
|
||||
|
func IsIPv4(ipAddr string, isCidr bool) bool { |
||||
|
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ".") |
||||
|
} |
||||
|
|
||||
|
func IsIPv6(ipAddr string, isCidr bool) bool { |
||||
|
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":") |
||||
|
} |
||||
|
|
||||
|
func JsonEncode(raw interface{}) string { |
||||
|
jsonOutput, _ := json.MarshalIndent(raw, "", " ") // json encode
|
||||
|
return string(jsonOutput) |
||||
|
} |
||||
|
|
||||
|
func RunCommand(command ...string) (int, string) { |
||||
|
log.Debugf("Running system command -> %v", command) |
||||
|
process := exec.Command(command[0], command[1:]...) |
||||
|
output, _ := process.CombinedOutput() |
||||
|
log.Debugf("Command %v -> \n%s", command, string(output)) |
||||
|
code := process.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() |
||||
|
if code != 0 { |
||||
|
log.Warningf("Command %v return code %d", command, code) |
||||
|
} |
||||
|
return code, string(output) |
||||
|
} |
@ -1,22 +0,0 @@ |
|||||
package common |
|
||||
|
|
||||
import ( |
|
||||
"net" |
|
||||
"strings" |
|
||||
) |
|
||||
|
|
||||
func isIP(ipAddr string, isCidr bool) bool { |
|
||||
if !isCidr { |
|
||||
return net.ParseIP(ipAddr) != nil |
|
||||
} |
|
||||
_, _, err := net.ParseCIDR(ipAddr) |
|
||||
return err == nil |
|
||||
} |
|
||||
|
|
||||
func IsIPv4(ipAddr string, isCidr bool) bool { |
|
||||
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ".") |
|
||||
} |
|
||||
|
|
||||
func IsIPv6(ipAddr string, isCidr bool) bool { |
|
||||
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":") |
|
||||
} |
|
@ -1,19 +0,0 @@ |
|||||
package common |
|
||||
|
|
||||
import ( |
|
||||
log "github.com/sirupsen/logrus" |
|
||||
"os/exec" |
|
||||
"syscall" |
|
||||
) |
|
||||
|
|
||||
func RunCommand(command ...string) (int, string) { |
|
||||
log.Debugf("Running system command -> %v", command) |
|
||||
process := exec.Command(command[0], command[1:]...) |
|
||||
output, _ := process.CombinedOutput() |
|
||||
log.Debugf("Command %v -> \n%s", command, string(output)) |
|
||||
code := process.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() |
|
||||
if code != 0 { |
|
||||
log.Warningf("Command %v return code %d", command, code) |
|
||||
} |
|
||||
return code, string(output) |
|
||||
} |
|
@ -1,37 +0,0 @@ |
|||||
package proxy |
|
||||
|
|
||||
type Config struct { |
|
||||
Sniff bool |
|
||||
Redirect bool |
|
||||
V4TProxyPort int |
|
||||
V6TProxyPort int |
|
||||
LogLevel string |
|
||||
HttpInbounds map[string]int |
|
||||
SocksInbounds map[string]int |
|
||||
AddOnInbounds []interface{} |
|
||||
} |
|
||||
|
|
||||
type logObject struct { |
|
||||
Loglevel string `json:"loglevel"` |
|
||||
Access string `json:"access"` |
|
||||
Error string `json:"error"` |
|
||||
} |
|
||||
|
|
||||
type inboundsObject struct { |
|
||||
Inbounds []interface{} `json:"inbounds"` |
|
||||
} |
|
||||
|
|
||||
type sniffObject struct { |
|
||||
Enabled bool `json:"enabled"` |
|
||||
RouteOnly bool `json:"routeOnly"` |
|
||||
DestOverride []string `json:"destOverride"` |
|
||||
} |
|
||||
|
|
||||
type inboundObject struct { |
|
||||
Tag string `json:"tag"` |
|
||||
Port int `json:"port"` |
|
||||
Protocol string `json:"protocol"` |
|
||||
Settings interface{} `json:"settings"` |
|
||||
StreamSettings interface{} `json:"streamSettings"` |
|
||||
Sniffing sniffObject `json:"sniffing"` |
|
||||
} |
|
Loading…
Reference in new issue