2021-10-23 04:56:24 +00:00
|
|
|
package configui
|
2021-10-18 10:59:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
2021-10-23 04:56:24 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-10-18 10:59:09 +00:00
|
|
|
"os"
|
2021-10-18 15:53:49 +00:00
|
|
|
"strings"
|
2021-10-18 10:59:09 +00:00
|
|
|
)
|
|
|
|
|
2021-10-19 04:34:01 +00:00
|
|
|
func matchIPGlob(ip, pattern string) bool {
|
|
|
|
parts := strings.Split(pattern, ".")
|
|
|
|
seg := strings.Split(ip, ".")
|
|
|
|
for i, part := range parts {
|
|
|
|
|
|
|
|
// normalize pattern to 3 digits
|
|
|
|
switch len(part) {
|
|
|
|
case 1:
|
|
|
|
if part == "*" {
|
|
|
|
part = "***"
|
|
|
|
} else {
|
|
|
|
part = "00" + part
|
2021-10-18 18:38:28 +00:00
|
|
|
}
|
2021-10-19 04:34:01 +00:00
|
|
|
case 2:
|
|
|
|
if strings.HasPrefix(part, "*") {
|
|
|
|
part = "*" + part
|
|
|
|
} else if strings.HasSuffix(part, "*") {
|
|
|
|
part = part + "*"
|
|
|
|
} else {
|
|
|
|
part = "0" + part
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 18:38:28 +00:00
|
|
|
|
2021-10-19 04:34:01 +00:00
|
|
|
// normalize ip to 3 digits
|
|
|
|
switch len(seg[i]) {
|
|
|
|
case 1:
|
|
|
|
seg[i] = "00" + seg[i]
|
|
|
|
case 2:
|
|
|
|
seg[i] = "0" + seg[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := range part {
|
|
|
|
if string(part[j]) == "*" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if part[j] != seg[i][j] {
|
|
|
|
return false
|
2021-10-18 18:38:28 +00:00
|
|
|
}
|
2021-10-19 04:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2021-10-18 10:59:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func bundle(buf io.Writer, paths []string, rootDir string, flat bool) error {
|
2021-10-18 10:59:09 +00:00
|
|
|
gw := gzip.NewWriter(buf)
|
|
|
|
defer gw.Close()
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
for _, file := range paths {
|
|
|
|
if err := func(file string) error {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
info, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := tar.FileInfoHeader(info, info.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use full path as name (FileInfoHeader only takes the basename)
|
|
|
|
// If we don't do this the directory strucuture would
|
|
|
|
// not be preserved
|
|
|
|
// https://golang.org/src/archive/tar/common.go?#L626
|
|
|
|
if !flat {
|
2021-10-18 15:53:49 +00:00
|
|
|
if !strings.HasPrefix(file, "/") {
|
2021-10-23 04:56:24 +00:00
|
|
|
file = rootDir + "/" + file
|
2021-10-18 15:53:49 +00:00
|
|
|
} else {
|
2021-10-23 04:56:24 +00:00
|
|
|
file = rootDir + file
|
2021-10-18 15:53:49 +00:00
|
|
|
}
|
2021-10-18 10:59:09 +00:00
|
|
|
header.Name = file
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write file header to the tar archive
|
|
|
|
err = tw.WriteHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy file content to tar archive
|
|
|
|
_, err = io.Copy(tw, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-20 15:58:14 +00:00
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func GetIP(r *http.Request) string {
|
|
|
|
ip := r.Header.Get("X-Real-Ip")
|
|
|
|
if ip == "" {
|
|
|
|
ips := r.Header.Get("X-Forwarded-For")
|
|
|
|
ipArr := strings.Split(ips, ",")
|
|
|
|
ip = strings.Trim(ipArr[len(ipArr)-1], " ")
|
|
|
|
}
|
|
|
|
if ip == "" {
|
|
|
|
var err error
|
|
|
|
ip, _, err = net.SplitHostPort(r.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
ip = r.RemoteAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip
|
2021-10-20 15:58:14 +00:00
|
|
|
}
|