diff --git a/README.md b/README.md index d87ee76..cd705c1 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ - +   #  **ONLINE TOOLS ([codetabs.com](https://codetabs.com))** -**version 0.8.3** +**version 0.8.4** 1. [Count LOC (lines of code) online from github/gitlab repos or zipped uploaded folder](#count-loc-online) 2. [CORS proxy](#cors-proxy) diff --git a/_utils/stats.go b/_utils/stats.go index 6c08264..50de2c9 100644 --- a/_utils/stats.go +++ b/_utils/stats.go @@ -26,7 +26,6 @@ func AddHit(w http.ResponseWriter, r *http.Request, service, mode string, } } } - askingFor := strings.Trim(fmt.Sprint(r.URL), string(r.URL.Path)) if mode == "production" { @@ -38,6 +37,41 @@ func AddHit(w http.ResponseWriter, r *http.Request, service, mode string, } } +// AddBanned ... +func AddBanned(w http.ResponseWriter, r *http.Request, service, mode string, + hLog *log.Logger) { + ip := GetIP(r) + sv := strings.ToUpper(service) + host := r.Header.Get("Host") + if host == "" { + host = r.Header.Get("Origin") + if host == "" { + host = r.Header.Get("Referer") + if host == "" { + host = "???" + } + } + } + askingFor := strings.Trim(fmt.Sprint(r.URL), string(r.URL.Path)) + + if mode == "production" { + hLog.Println(ip, sv, host, askingFor) + } else { + log.Println(ip, sv, host, askingFor) + } +} + +// CreateCustomBansLogFile ... +func NewBanFile(f string) *log.Logger { + infoLog, err := os.OpenFile(f, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + log.Fatalf("ERROR opening Ban log file %s\n", err) + } + bannedLog := log.New(infoLog, "BANNED: ", log.Ldate|log.Ltime) + //hitsLog := log.New(infoLog, "HIT :\t", log.Ldate|log.Ltime) + return bannedLog +} + // CreateCustomHitsLogFile ... func NewHitsFile(f string) *log.Logger { infoLog, err := os.OpenFile(f, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) diff --git a/main.go b/main.go index 54ff58f..c3ed233 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ import ( "github.com/jolav/codetabs/weather" ) -var version = "0.8.3" +var version = "0.8.4" var when = "undefined" type Conf struct { @@ -34,9 +34,11 @@ type Conf struct { Port int ErrorsLogFile string HitsLogFile string + BannedLogFile string DevHosts []string Services []string hitsLog *log.Logger + bannedLog *log.Logger } func main() { @@ -68,6 +70,10 @@ func main() { } defer mylog.Close() + // Custom Ban Log File + c.BannedLogFile = usernow.HomeDir + c.BannedLogFile + c.bannedLog = u.NewBanFile(c.BannedLogFile) + // Custom Hits Log File c.HitsLogFile = usernow.HomeDir + c.HitsLogFile c.hitsLog = u.NewHitsFile(c.HitsLogFile) @@ -86,6 +92,7 @@ func main() { mux.HandleFunc("/v1/video2gif/", mw(video2gif.Router, "video2gif", c)) mux.HandleFunc("/v1/stars/", mw(stars.Router, "stars", c)) mux.HandleFunc("/v1/proxy/", mw(proxy.Router, "proxy", c)) + mux.HandleFunc("/v1/tmp/", mw(proxy.Router, "proxy", c)) mux.HandleFunc("/v1/loc/", mw(loc.Router, "loc", c)) mux.HandleFunc("/", u.BadRequest) @@ -104,14 +111,14 @@ func main() { func mw(next http.HandlerFunc, service string, c Conf) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - go u.AddHit(w, r, service, c.Mode, c.hitsLog) if service == "proxy" { if isBanned(r) { http.Error(w, "Unauthorized", http.StatusUnauthorized) + go u.AddBanned(w, r, service, c.Mode, c.bannedLog) return } } - + go u.AddHit(w, r, service, c.Mode, c.hitsLog) next.ServeHTTP(w, r) }) } diff --git a/proxy/proxy.go b/proxy/proxy.go index 0bd082a..0fac474 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -71,6 +71,11 @@ func (p *proxy) doProxyRequest(w http.ResponseWriter, r *http.Request) { if strings.Contains(contentType, "application/xml") { u.SendXMLToClient(w, data, 200) + return + } + + if strings.Contains(contentType, "text/") { + w.Header().Add("Content-type", "text/plain") } reader := bufio.NewReader(resp.Body) diff --git a/www/_error/404.css b/www/_error/404.css index 62938c1..fb3011f 100644 --- a/www/_error/404.css +++ b/www/_error/404.css @@ -88,7 +88,7 @@ a.link:hover { /* TITLE */ .titleBox { - font-family: "Titillium Web", "arial", sans-serif; + font-family: "arial", sans-serif; padding: 0px; font-size: 2.5em; text-align: center; @@ -98,4 +98,4 @@ a.link:hover { .titleBox img { vertical-align: middle; padding: 0px 15px 8px 0px; -} \ No newline at end of file +} diff --git a/www/_error/404.html b/www/_error/404.html index 6cefafd..f6e1049 100644 --- a/www/_error/404.html +++ b/www/_error/404.html @@ -46,12 +46,6 @@