Changeset 70 in code
- Timestamp:
- Feb 12, 2024, 2:42:11 AM (16 months ago)
- Location:
- trunk
- Files:
-
- 2 added
- 1 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/INSTANCES.md
r67 r70 1 1 # List of known instances 2 2 3 * [tr.chaotic.ninja](https://tr.chaotic.ninja) 4 * Location: Germany 5 * Cloudflare: No 6 3 | Name | Cloudflare? | Country | URL | Engines supported | 4 |---------------------------------------------|-------------|---------|--------------------------|-------------------| 5 | Chaotic Ninja Communication Network Limited | No | Germany | https://tr.chaotic.ninja | Google, Reverso | 6 | | | | | | -
trunk/README.md
r67 r70 8 8 ### Other projects 9 9 * [Mozhi](https://codeberg.org/aryak/mozhi), also a fork of SimplyTranslate 10 11 ### Credits 12 * SimpleWeb (fattalion, ManeraKai, metalune) 13 * [VnPower](https://vnpower.exozy.me) ([PixivFE](https://codeberg.org/VnPower/PixivFE) developer), for most of the stuff I borrowed from his project, as we both use [Fiber](https://gofiber.io) 14 * [z3bra](http://z3bra.org), for the `.ini` loading function, and privilege dropping from [Partage](http://z3bra.org/partage/) -
trunk/cmd/mai/main.go
r68 r70 3 3 import ( 4 4 "bytes" 5 "fmt" 5 "errors" 6 "log" 6 7 "net/http" 7 8 "net/url" … … 9 10 "time" 10 11 "runtime" 12 "strings" 11 13 "syscall" 12 14 … … 18 20 "github.com/gofiber/fiber/v2/middleware/logger" 19 21 "github.com/gofiber/fiber/v2/middleware/limiter" 22 "github.com/gofiber/fiber/v2/middleware/recover" 20 23 "github.com/gofiber/template/html/v2" 21 24 ) … … 26 29 ) 27 30 var conf struct { 31 danmaku int 28 32 listen string 29 33 staticpath string 30 34 tmplpath string 31 35 } 36 func MaiSkipLimiter(c *fiber.Ctx) bool { 37 // Paths listed here are not considered for rate limiting 38 path := c.Path() 39 return strings.HasPrefix(path, "/static") || 40 strings.HasPrefix(path, "/docs") 41 } 32 42 func main() { 33 43 parseFlags() … … 38 48 39 49 // Default settings 50 conf.danmaku = 10 40 51 conf.listen = "127.0.0.1:5000" 41 52 conf.staticpath = "./static" … … 45 56 uid, gid, err := usergroupids(username, groupname) 46 57 if err != nil { 47 fmt.Println(err) 48 os.Exit(1) 58 log.Fatal(err) 49 59 } 50 60 syscall.Setuid(uid) … … 55 65 engine.AddFunc("inc", func(i int) int { return i + 1 }) 56 66 57 app:= fiber.New(67 server := fiber.New( 58 68 fiber.Config{ 59 69 AppName: "Mai", … … 62 72 ServerHeader: "Mai (using Fiber v2.x)", 63 73 Views: engine, 64 }) 65 66 app.Use(favicon.New( 74 ErrorHandler: func(c *fiber.Ctx, err error) error { 75 code := fiber.StatusInternalServerError 76 err = c.Status(code).Render("pages/error", fiber.Map{"Title": "Error", "Error": err}) 77 if err != nil { 78 return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") 79 } 80 return nil 81 }, 82 }) 83 84 server.Use(recover.New()) 85 86 server.Use(favicon.New( 67 87 favicon.Config{ 68 88 File: conf.staticpath + "/favicon.ico", … … 70 90 )) 71 91 72 app.Use(logger.New(92 server.Use(logger.New( 73 93 logger.Config{ 74 94 Format: "==> ${ip}:${port} ${status} - ${method} ${path}\n", 75 95 DisableColors: true, 76 96 Output: os.Stdout, 97 Next: MaiSkipLimiter, 77 98 }, 78 99 )) 79 100 80 app.Use(limiter.New(limiter.Config{ 81 Max: 10, 82 Expiration: 180 * time.Second, 101 server.Use(limiter.New(limiter.Config{ 102 Next: MaiSkipLimiter, 103 Max: conf.danmaku, 104 Expiration: 30 * time.Second, 105 LimiterMiddleware: limiter.SlidingWindow{}, 83 106 LimitReached: func(c *fiber.Ctx) error { 84 return c.SendStatus(429)85 return c.SendFile(conf.tmplpath + "/429.html")107 log.Println("Limit reached!") 108 return errors.New("You're firing way too many danmaku really fast!") 86 109 }, 87 110 })) 88 111 89 app.All("/", func(c *fiber.Ctx) error {112 server.All("/", func(c *fiber.Ctx) error { 90 113 engine := c.Cookies("engine") 91 114 if c.Query("engine") != "" { … … 186 209 }) 187 210 188 app.All("/api/translate", func(c *fiber.Ctx) error {211 server.All("/api/translate", func(c *fiber.Ctx) error { 189 212 from := "" 190 213 to := "" … … 217 240 }) 218 241 219 app.Get("/api/source_languages", func(c *fiber.Ctx) error {242 server.Get("/api/source_languages", func(c *fiber.Ctx) error { 220 243 engine := c.Query("engine") 221 244 if _, ok := engines.Engines[engine]; !ok || engine == "" { … … 229 252 }) 230 253 231 app.Get("/api/target_languages", func(c *fiber.Ctx) error {254 server.Get("/api/target_languages", func(c *fiber.Ctx) error { 232 255 engine := c.Query("engine") 233 256 if _, ok := engines.Engines[engine]; !ok || engine == "" { … … 241 264 }) 242 265 243 app.Get("/api/tts", func(c *fiber.Ctx) error {266 server.Get("/api/tts", func(c *fiber.Ctx) error { 244 267 engine := c.Query("engine") 245 268 if _, ok := engines.Engines[engine]; !ok || engine == "" { … … 268 291 } 269 292 }) 270 app.Get("/robots.txt", func(c *fiber.Ctx) error { 293 294 server.Get("/robots.txt", func(c *fiber.Ctx) error { 271 295 return c.SendString("User-Agent: *\nDisallow: /\n") 272 296 }) 273 app.Get("/version", func(c *fiber.Ctx) error { 297 298 server.Get("/toomanyrequests", func(c *fiber.Ctx) error { 299 return c.SendFile(conf.tmplpath + "/429.html") 300 return c.SendStatus(429) 301 }) 302 303 server.Get("/version", func(c *fiber.Ctx) error { 274 304 return c.JSON(fiber.Map{ 275 305 "fiberversion": fiber.Version, … … 278 308 }) 279 309 }) 280 app.Post("/switchlanguages", func(c *fiber.Ctx) error { 310 311 server.Post("/switchlanguages", func(c *fiber.Ctx) error { 281 312 if c.Cookies("from") != "" { 282 313 fromCookie := new(fiber.Cookie) … … 295 326 return c.Redirect("/") 296 327 }) 297 app.Static("/static", conf.staticpath, fiber.Static{328 server.Static("/static", conf.staticpath, fiber.Static{ 298 329 Compress: true, 299 330 ByteRange: true, 300 331 Browse: true, 301 332 }) 302 app.Static("/docs", "./docs", fiber.Static{}) 303 app.Listen(conf.listen) 333 334 server.Static("/docs", "./docs", fiber.Static{}) 335 336 server.Listen(conf.listen) 304 337 } -
trunk/cmd/mai/readconf.go
r67 r70 11 11 return err 12 12 } 13 conf.danmaku, _ = cfg.Section("mai").Key("danmaku").Int() 13 14 conf.listen = cfg.Section("mai").Key("listen").String() 14 15 conf.staticpath = cfg.Section("mai").Key("static").String() -
trunk/go.mod
r61 r70 5 5 require ( 6 6 github.com/PuerkitoBio/goquery v1.8.1 7 github.com/gofiber/fiber/v2 v2.5 0.07 github.com/gofiber/fiber/v2 v2.52.0 8 8 github.com/gofiber/template/html/v2 v2.0.5 9 9 gopkg.in/ini.v1 v1.67.0 … … 15 15 github.com/gofiber/template v1.8.2 // indirect 16 16 github.com/gofiber/utils v1.1.0 // indirect 17 github.com/google/uuid v1. 3.1// indirect18 github.com/klauspost/compress v1.1 6.7// indirect17 github.com/google/uuid v1.5.0 // indirect 18 github.com/klauspost/compress v1.17.0 // indirect 19 19 github.com/mattn/go-colorable v0.1.13 // indirect 20 github.com/mattn/go-isatty v0.0. 19// indirect20 github.com/mattn/go-isatty v0.0.20 // indirect 21 21 github.com/mattn/go-runewidth v0.0.15 // indirect 22 22 github.com/philhofer/fwd v1.1.2 // indirect … … 25 25 github.com/tinylib/msgp v1.1.8 // indirect 26 26 github.com/valyala/bytebufferpool v1.0.0 // indirect 27 github.com/valyala/fasthttp v1.5 0.0 // indirect27 github.com/valyala/fasthttp v1.51.0 // indirect 28 28 github.com/valyala/tcplisten v1.0.0 // indirect 29 golang.org/x/net v0. 8.0 // indirect30 golang.org/x/sys v0.1 3.0 // indirect29 golang.org/x/net v0.17.0 // indirect 30 golang.org/x/sys v0.15.0 // indirect 31 31 ) -
trunk/go.sum
r61 r70 6 6 github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= 7 7 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 8 github.com/gofiber/fiber/v2 v2.5 0.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw=9 github.com/gofiber/fiber/v2 v2.5 0.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw=8 github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE= 9 github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= 10 10 github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk= 11 11 github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8= … … 14 14 github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM= 15 15 github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0= 16 github.com/google/uuid v1. 3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=17 github.com/google/uuid v1. 3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=18 github.com/klauspost/compress v1.1 6.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=19 github.com/klauspost/compress v1.1 6.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=16 github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= 17 github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 18 github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= 19 github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= 20 20 github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 21 21 github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 22 22 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 23 github.com/mattn/go-isatty v0.0. 19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=24 github.com/mattn/go-isatty v0.0. 19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=23 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 24 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 25 25 github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= 26 26 github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= … … 36 36 github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 37 37 github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 38 github.com/valyala/fasthttp v1.5 0.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=39 github.com/valyala/fasthttp v1.5 0.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=38 github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= 39 github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= 40 40 github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 41 41 github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= … … 51 51 golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= 52 52 golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 53 golang.org/x/net v0. 8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=54 golang.org/x/net v0. 8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=53 golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= 54 golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= 55 55 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 56 56 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= … … 66 66 golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 67 67 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 68 golang.org/x/sys v0.1 3.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=69 golang.org/x/sys v0.1 3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=68 golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= 69 golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 70 70 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 71 71 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -
trunk/mai.ini.5
r67 r70 8 8 .Ss [mai] section 9 9 .Bl -tag -width 11n 10 .It listen 10 .It danmaku (int) 11 Control the maximum amount of requests 12 before a ratelimit happens 13 .It listen (string) 11 14 HTTP port for the server to listen. 12 15 Default is "localhost:5000" 13 .It static 16 .It static (string) 14 17 Directory where the static resources are located. 15 18 Default is "./static" 16 .It templates 19 .It templates (string) 17 20 Directory where the templates are located. 18 21 Default is "./views"
Note:
See TracChangeset
for help on using the changeset viewer.