This commit is contained in:
lost+skunk 2024-09-23 09:38:32 +03:00
parent c39399403e
commit 191984b31e
19 changed files with 230 additions and 124 deletions

View file

@ -9,69 +9,68 @@ import (
)
type API struct {
main *skunkyart
main *skunkyart
}
type info struct {
Version string `json:"version"`
Settings settingsParams `json:"settings"`
Version string `json:"version"`
Settings settingsParams `json:"settings"`
}
func (a API) Info() {
json, err := json.Marshal(info{
Version: a.main.Version,
Settings: settingsParams{
Nsfw: CFG.Nsfw,
Proxy: CFG.Proxy,
},
})
try(err)
a.main.Writer.Write(json)
return
json, err := json.Marshal(info{
Version: a.main.Version,
Settings: settingsParams{
Nsfw: CFG.Nsfw,
Proxy: CFG.Proxy,
},
})
try(err)
a.main.Writer.Write(json)
}
func (a API) Error(description string, status int) {
a.main.Writer.WriteHeader(status)
var response strings.Builder
response.WriteString(`{"error":"`)
response.WriteString(description)
response.WriteString(`"}`)
wr(a.main.Writer, response.String())
a.main.Writer.WriteHeader(status)
var response strings.Builder
response.WriteString(`{"error":"`)
response.WriteString(description)
response.WriteString(`"}`)
wr(a.main.Writer, response.String())
}
func (a API) sendMedia(d *devianter.Deviation) {
mediaUrl, name := devianter.UrlFromMedia(d.Media)
mediaUrl, name := devianter.UrlFromMedia(d.Media)
a.main.SetFilename(name)
if len(mediaUrl) != 0 {
mediaUrl = mediaUrl[21:]
if len(mediaUrl) != 0 {
mediaUrl = mediaUrl[21:]
dot := strings.Index(mediaUrl, ".")
a.main.Writer.Header().Del("Content-Type")
a.main.DownloadAndSendMedia(mediaUrl[:dot], mediaUrl[dot+11:])
}
a.main.DownloadAndSendMedia(mediaUrl[:dot], mediaUrl[dot+11:])
}
}
// TODO: сделать фильтры
func (a API) Random() {
for attempt := 1;; {
if attempt > 3 {
a.Error("Sorry, butt NSFW on this are disabled, and the instance failed to find a random art without NSFW", 500)
}
s, err, daErr := devianter.PerformSearch(string(rand.Intn(999)), rand.Intn(30), 'a')
try(err)
if daErr.RAW != nil {
continue
}
deviation := &s.Results[rand.Intn(len(s.Results))]
if deviation.NSFW && !CFG.Nsfw {
attempt++
continue
}
a.sendMedia(deviation)
return
}
for attempt := 1; ; {
if attempt > 3 {
a.Error("Sorry, butt NSFW on this are disabled, and the instance failed to find a random art without NSFW", 500)
}
s, err, daErr := devianter.PerformSearch(string(rand.Intn(999)), rand.Intn(30), 'a')
try(err)
if daErr.RAW != nil {
continue
}
deviation := &s.Results[rand.Intn(len(s.Results))]
if deviation.NSFW && !CFG.Nsfw {
attempt++
continue
}
a.sendMedia(deviation)
return
}
}

View file

@ -27,10 +27,10 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
url.WriteString(".wixmp.com/")
url.WriteString(path)
if t := s.Args.Get("token"); t != "" {
url.WriteString("?token=")
url.WriteString(t)
url.WriteString("?token=")
url.WriteString(t)
}
var response []byte
switch {
@ -127,7 +127,7 @@ func InitCacheSystem() {
try(os.RemoveAll(fileName))
}
}
if c.MaxSize != 0 && fileInfo.Size() > c.MaxSize {
try(os.RemoveAll(fileName))
}

View file

@ -56,13 +56,11 @@ func ExecuteConfig() {
if CFG.cfg != "" {
f, err := os.ReadFile(CFG.cfg)
tryWithExitStatus(err, 1)
tryWithExitStatus(json.Unmarshal(f, &CFG), 1)
if CFG.Cache.Enabled && !CFG.Proxy {
exit("Incompatible settings detected: cannot use caching media content without proxy", 1)
}
static.StaticPath = CFG.StaticPath
if CFG.Cache.Enabled {
if CFG.Cache.Lifetime != "" {
var duration int64
@ -92,6 +90,13 @@ func ExecuteConfig() {
CFG.Cache.MaxSize *= 1024 ^ 2
go InitCacheSystem()
}
About = instanceAbout{
Proxy: CFG.Proxy,
Nsfw: CFG.Nsfw,
}
static.StaticPath = CFG.StaticPath
devianter.UserAgent = CFG.UserAgent
}
}

View file

@ -3,7 +3,7 @@ package app
import (
"io"
"net/http"
u "net/url"
url "net/url"
"skunkyart/static"
"strconv"
"strings"
@ -57,7 +57,7 @@ func Router() {
Path = r.URL.Path
path := parsepath(Path)
Host = "http://" + r.Host
if h := r.Header["X-Forwarded-Proto"]; len(h) != 0 && h[0] == "https" {
Host = "https://" + r.Host
}
@ -67,13 +67,13 @@ func Router() {
skunky.Args = r.URL.Query()
arg := skunky.Args.Get
p, _ := strconv.Atoi(arg("p"))
skunky.Endpoint = path[1]
skunky.API.main = &skunky
skunky.Writer = w
skunky.BasePath = CFG.URI
skunky.QueryRaw = arg("q")
skunky.Query = u.QueryEscape(skunky.QueryRaw)
skunky.Query = url.QueryEscape(skunky.QueryRaw)
skunky.Page = p
if t := arg("type"); len(t) > 0 {
@ -84,12 +84,21 @@ func Router() {
skunky.Atom = true
}
if CFG.Proxy {
w.Header().Add("Content-Security-Policy", "default-src 'self'; script-src 'none'; style-src 'self' 'unsafe-inline'")
} else {
w.Header().Add("Content-Security-Policy", "default-src 'self'; img-src 'self' *.wixmp.com; script-src 'none'; style-src 'self' 'unsafe-inline'")
}
w.Header().Add("X-Frame-Options", "DENY")
switch skunky.Endpoint {
// main
case "":
skunky.ExecuteTemplate("index.htm", "html", &CFG.URI)
case "about":
skunky.About()
skunky.Templates.About = About
skunky.ExecuteTemplate("about.htm", "html", &skunky)
case "post":
skunky.Deviation(path[2], path[3])
case "search":
@ -120,12 +129,12 @@ func Router() {
case "api":
w.Header().Add("Content-Type", "application/json")
switch path[2] {
case "instance":
skunky.API.Info()
case "random":
skunky.API.Random()
default:
skunky.API.Error("Not Found", 404)
case "instance":
skunky.API.Info()
case "random":
skunky.API.Random()
default:
skunky.API.Error("Not Found", 404)
}
// 404

View file

@ -1,6 +1,7 @@
package app
import (
"encoding/json"
"io"
"net/http"
"net/url"
@ -40,18 +41,26 @@ func restore() {
}
var instances []byte
var About instanceAbout
func RefreshInstances() {
for {
func() {
defer restore()
instances = Download("https://git.macaw.me/skunky/SkunkyArt/raw/branch/master/instances.json").Body
try(json.Unmarshal(instances, &About))
}()
time.Sleep(1 * time.Hour)
}
}
// some crap for frontend
type instanceAbout struct {
Proxy bool
Nsfw bool
Instances []settings
}
type skunkyart struct {
Writer http.ResponseWriter
@ -63,15 +72,11 @@ type skunkyart struct {
BasePath, Endpoint string
Query, QueryRaw string
API API
Version string
API API
Version string
Templates struct {
About struct {
Proxy bool
Nsfw bool
Instances []settings
}
About instanceAbout
SomeList string
DDStrips string
@ -132,7 +137,7 @@ func UrlBuilder(strs ...string) string {
str.WriteString(CFG.URI)
for n, x := range strs {
str.WriteString(x)
if n := n+1; n < l && len(strs[n]) != 0 && !(strs[n][0] == '?' || strs[n][0] == '&') && !(x[0] == '?' || x[0] == '&') {
if n := n + 1; n < l && len(strs[n]) != 0 && !(strs[n][0] == '?' || strs[n][0] == '&') && !(x[0] == '?' || x[0] == '&') {
str.WriteString("/")
}
}
@ -215,6 +220,8 @@ func ParseMedia(media devianter.Media, thumb ...int) string {
filename = "image.gif"
}
return UrlBuilder("media", "file", mediaUrl[:dot], mediaUrl[dot+11:], "&filename=", filename)
} else if !CFG.Proxy {
return mediaUrl
}
return ""
}

View file

@ -1,7 +1,6 @@
package app
import (
"encoding/json"
"regexp"
"strconv"
"strings"
@ -24,8 +23,8 @@ func (s skunkyart) GRUser() {
s.Templates.GroupUser.GR, err, daError = g.Get()
try(err)
if daError.RAW != nil {
s.Error(daError)
return
s.Error(daError)
return
}
group := &s.Templates.GroupUser
@ -68,7 +67,7 @@ func (s skunkyart) GRUser() {
group.About.Interests += interest.String()
}
}
group.About.Comments = s.ParseComments(devianter.GetComments(strconv.Itoa(group.GR.Gruser.ID),"",s.Page,4))
group.About.Comments = s.ParseComments(devianter.GetComments(strconv.Itoa(group.GR.Gruser.ID), "", s.Page, 4))
case "cover_deviation":
group.About.BGMeta = x.ModuleData.CoverDeviation.Deviation
@ -225,8 +224,8 @@ func (s skunkyart) Deviation(author, postname string) {
func (s skunkyart) DD() {
dd, err := devianter.GetDailyDeviations(s.Page)
if err.RAW != nil {
s.Error(err)
return
s.Error(err)
return
}
var strips strings.Builder
for _, x := range dd.Strips {
@ -312,12 +311,13 @@ func (s skunkyart) Search() {
return
}
try(err)
if daError.RAW != nil {
s.Error(daError)
return
}
if s.Type != 'r' {
if daError.RAW != nil {
s.Error(daError)
return
}
ss.List = s.DeviationList(ss.Content.Results, false, DeviationList{
Pages: ss.Content.Pages,
More: ss.Content.HasMore,
@ -339,10 +339,3 @@ func (s skunkyart) Emojitar(name string) {
}
wr(s.Writer, ae)
}
func (s skunkyart) About() {
s.Templates.About.Nsfw = CFG.Nsfw
s.Templates.About.Proxy = CFG.Proxy
try(json.Unmarshal(instances, &s.Templates.About))
s.ExecuteTemplate("about.htm", "html", &s)
}