Fix vendor
parent
81782bb794
commit
75a65cecd2
|
|
@ -1,98 +0,0 @@
|
||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var outfile *os.File
|
|
||||||
var outfileB *os.File
|
|
||||||
|
|
||||||
var lastWasReturn = false
|
|
||||||
|
|
||||||
var ErrCantConfigLog = errors.New("cannot configure log")
|
|
||||||
|
|
||||||
func Config(path string) error {
|
|
||||||
if path == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
||||||
if err != nil {
|
|
||||||
return ErrCantConfigLog
|
|
||||||
}
|
|
||||||
outfile = f
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Logf(args ...interface{}) {
|
|
||||||
if outfile == nil {
|
|
||||||
outfile = os.Stderr
|
|
||||||
}
|
|
||||||
defer outfile.Sync()
|
|
||||||
if len(args) == 0 {
|
|
||||||
fmt.Fprintln(outfile, "")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
v := fmt.Sprintf(args[0].(string), args[1:]...)
|
|
||||||
if v[len(v)-1] == '\n' {
|
|
||||||
v = v[:len(v)-1]
|
|
||||||
}
|
|
||||||
thisWasReturn := v[0] == '\r'
|
|
||||||
if lastWasReturn && !thisWasReturn {
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
if thisWasReturn {
|
|
||||||
fmt.Fprintf(outfile, "\r%s%v", prefix(), v[1:])
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(outfile, "%s%v\n", prefix(), v)
|
|
||||||
}
|
|
||||||
lastWasReturn = thisWasReturn
|
|
||||||
}
|
|
||||||
|
|
||||||
func Fatalf(args ...interface{}) {
|
|
||||||
Logf(args...)
|
|
||||||
if len(args) > 0 {
|
|
||||||
panic(args[0])
|
|
||||||
} else {
|
|
||||||
panic("Panic from logger.fatal()")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Fatal(args ...interface{}) {
|
|
||||||
Log(args...)
|
|
||||||
if len(args) > 0 {
|
|
||||||
panic(args[0])
|
|
||||||
} else {
|
|
||||||
panic("Panic from logger.fatal()")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Log(args ...interface{}) {
|
|
||||||
if lastWasReturn {
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
if outfile == nil {
|
|
||||||
outfile = os.Stderr
|
|
||||||
}
|
|
||||||
v := fmt.Sprintf("%v", args)
|
|
||||||
v = v[1 : len(v)-1]
|
|
||||||
fmt.Fprintf(outfile, "%s%v\n", prefix(), v)
|
|
||||||
outfile.Sync()
|
|
||||||
lastWasReturn = false
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogB(args ...interface{}) {
|
|
||||||
if outfileB == nil {
|
|
||||||
outfileB = os.Stderr
|
|
||||||
}
|
|
||||||
v := fmt.Sprintf("%v", args)
|
|
||||||
v = v[1 : len(v)-1]
|
|
||||||
fmt.Fprintf(outfileB, "%s%v\n", prefix(), v)
|
|
||||||
outfileB.Sync()
|
|
||||||
}
|
|
||||||
|
|
||||||
func prefix() string {
|
|
||||||
return time.Now().Format("20060102-150405") + " : "
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
runtime: go
|
|
||||||
api_version: go1
|
|
||||||
handlers:
|
|
||||||
- url: /.*
|
|
||||||
script: _go_app
|
|
||||||
secure: always
|
|
||||||
|
|
@ -1,134 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"encoding/csv"
|
|
||||||
"flag"
|
|
||||||
"local/logger"
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"google.golang.org/appengine"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
exePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
exePath = "."
|
|
||||||
|
|
||||||
directory := flag.String("d", path.Join(exePath, "public"), "the directory of static file to host")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
getIPs()
|
|
||||||
|
|
||||||
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
remoteIP := strings.Split(r.RemoteAddr, ":")[0]
|
|
||||||
if notUSA(remoteIP) {
|
|
||||||
logger.Log(remoteIP, "NOT USA")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if r.URL.Scheme == "http" || strings.HasPrefix(r.Host, "http:") {
|
|
||||||
r.URL.Scheme = "https"
|
|
||||||
http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
http.FileServer(http.Dir(*directory)).ServeHTTP(w, r)
|
|
||||||
}))
|
|
||||||
|
|
||||||
log.Printf("Serving %s\n", *directory)
|
|
||||||
|
|
||||||
appengine.Main()
|
|
||||||
}
|
|
||||||
|
|
||||||
var globalIPs []uint64
|
|
||||||
|
|
||||||
func getIPs() []uint64 {
|
|
||||||
if globalIPs != nil {
|
|
||||||
return globalIPs
|
|
||||||
}
|
|
||||||
|
|
||||||
globalIPs = make([]uint64, 0)
|
|
||||||
|
|
||||||
f, err := os.Open("private/ipv4.csv")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
ipv4r := csv.NewReader(f)
|
|
||||||
ipv4all, err := ipv4r.ReadAll()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Log("IPV4s")
|
|
||||||
for i := range ipv4all {
|
|
||||||
if ipv4all[i][2] == "US" || ipv4all[i][2] == "-" {
|
|
||||||
start, err := strconv.ParseUint(ipv4all[i][0], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
stop, err := strconv.ParseUint(ipv4all[i][1], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
globalIPs = append(globalIPs, uint64(start), uint64(stop))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
g, err := os.Open("private/ipv6.csv")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ipv6r := csv.NewReader(g)
|
|
||||||
ipv6all, err := ipv6r.ReadAll()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Log("IPV6s")
|
|
||||||
for i := range ipv6all {
|
|
||||||
if ipv6all[i][2] == "US" {
|
|
||||||
start, err := strconv.ParseUint(ipv6all[i][0], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
stop, err := strconv.ParseUint(ipv6all[i][1], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
globalIPs = append(globalIPs, uint64(start), uint64(stop))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Slice(globalIPs, func(i, j int) bool {
|
|
||||||
return globalIPs[i] > globalIPs[j]
|
|
||||||
})
|
|
||||||
return globalIPs
|
|
||||||
}
|
|
||||||
|
|
||||||
func notUSA(ip string) bool {
|
|
||||||
dec := toDec(ip)
|
|
||||||
ips := getIPs()
|
|
||||||
n := sort.Search(len(ips), func(i int) bool {
|
|
||||||
return ips[i] > dec
|
|
||||||
})
|
|
||||||
logger.Log(ip, dec, ips[0], n, len(ips))
|
|
||||||
return n%2 == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDec(ips string) uint64 {
|
|
||||||
ip := net.ParseIP(ips)
|
|
||||||
if ip == nil {
|
|
||||||
return uint64(0)
|
|
||||||
}
|
|
||||||
if len(ip) == 16 {
|
|
||||||
return uint64(binary.BigEndian.Uint32(ip[12:16]))
|
|
||||||
}
|
|
||||||
return uint64(binary.BigEndian.Uint32(ip))
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func Test_notUSA(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
ip string
|
|
||||||
ok bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
ip: "8.8.8.8",
|
|
||||||
ok: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ip: "192.168.0.86",
|
|
||||||
ok: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ip: "127.0.0.1",
|
|
||||||
ok: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ip: "223.144.0.0",
|
|
||||||
ok: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range cases {
|
|
||||||
if notUSA(c.ip) != c.ok {
|
|
||||||
t.Errorf("WRONG VALIDATION for %v, expected %v", c.ip, c.ok)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue