diff --git a/config.json b/config.json index f142541..743e590 100644 --- a/config.json +++ b/config.json @@ -2,7 +2,7 @@ "HOST": "127.0.0.1", "PORT": "3333", "QUALITY": "80", - "IMG_PATH": "/path/to/pics", + "IMG_PATH": "./pics", "EXHAUST_PATH": "", - "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif"] + "ALLOWED_TYPES": ["jpg","png","jpeg","bmp"] } \ No newline at end of file diff --git a/go.mod b/go.mod index c5844a6..40503a6 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,6 @@ go 1.13 require ( github.com/chai2010/webp v1.1.0 github.com/gofiber/fiber v1.4.0 - github.com/sirupsen/logrus v1.4.2 + github.com/sirupsen/logrus v1.6.0 golang.org/x/image v0.0.0-20200119044424-58c23975cae1 ) diff --git a/helper.go b/helper.go index 6ede512..324bab8 100644 --- a/helper.go +++ b/helper.go @@ -2,11 +2,13 @@ package main import ( "fmt" + "github.com/gofiber/fiber" log "github.com/sirupsen/logrus" "net/http" "os" "path" "path/filepath" + "strings" ) func ChanErr(ccc chan int) { @@ -59,3 +61,45 @@ func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, req WebpAbsolutePath := path.Clean(path.Join(ExhaustPath, path.Dir(reqURI), WebpFilename)) return cwd, WebpAbsolutePath } + +func CheckUA(c *fiber.Ctx, RawImageAbs string) (string, bool) { + // reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox + // https://developer.chrome.com/multidevice/user-agent#chrome_for_ios_user_agent + // Chrome + // ✅ Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 + // ✅ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 + // ✅ Linux: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 + // ✅ iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.63 Mobile/15E148 Safari/604.1 + // ✅ Android: Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.60 Mobile Safari/537.36 + + // Firefox + // ✅ Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0 + // ✅ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:76.0) Gecko/20100101 Firefox/76.0 + // ✅ Linux: Mozilla/5.0 (X11; Linux i686; rv:76.0) Gecko/20100101 Firefox/76.0 + // ✅ iOS: Mozilla/5.0 (iPad; CPU OS 10_15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/25.0 Mobile/15E148 Safari/605.1.15 + // ✅ Android: Mozilla/5.0 (Android 10; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0 + + // Safari + // ❎ macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15 + // ❎ iOS: Mozilla/5.0 (iPad; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1 + + // WeChat + // ❎ iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 wxwork/2.1.5 MicroMessenger/6.3.22 + // ✅ Windows: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.691.400 QQBrowser/9.0.2524.400 + // ✅ Android: Mozilla/5.0 (Linux; Android 7.0; LG-H831 Build/NRD90U; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36 MicroMessenger/6.6.7.1303(0x26060743) NetType/WIFI Language/zh_TW + + UA := c.Get("User-Agent") + + if strings.Contains(UA, "Firefox") || strings.Contains(UA, "Chrome") { + // Chrome or firefox on macOS Windows + } else if strings.Contains(UA, "Android") || strings.Contains(UA, "Windows") || strings.Contains(UA, "Linux") { + // on Android, Windows and Linux + } else if strings.Contains(UA, "FxiOS") || strings.Contains(UA, "CriOS") { + //firefox and Chrome on iOS + } else { + log.Infof("A Safari user has arrived...%s", UA) + c.SendFile(RawImageAbs) + return "", true + } + return UA, false +} diff --git a/router.go b/router.go index 7c7b60a..a99f1b1 100644 --- a/router.go +++ b/router.go @@ -19,11 +19,9 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY var RawImageAbs = path.Join(ImgPath, reqURI) // /home/xxx/mypic/123.jpg var ImgFilename = path.Base(reqURI) // pure filename, 123.jpg var finalFile string // We'll only need one c.sendFile() - // Check for Safari users. If they're Safari or the UA contains `AppleWebKit`(Might be the WeChat Browser), just simply ignore everything. - UA := c.Get("User-Agent") - if (strings.Contains(UA, "Safari") && !strings.Contains(UA, "Chrome") && !strings.Contains(UA, "Firefox")) || (strings.Contains(UA, "AppleWebKit")) { - log.Info("A Safari user has arrived...") - c.SendFile(RawImageAbs) + + UA, done := CheckUA(c, RawImageAbs) + if done { return } log.Debugf("Incoming connection from %s@%s with %s", UA, c.IP(), ImgFilename)