diff --git a/.vscode/launch.json b/.vscode/launch.json index d981967..d67480d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -22,7 +22,7 @@ "mode": "auto", "program": "${workspaceRoot}", "args": ["serve", "--assets-host", "http://localhost:8082"], - "envFile": "${workspaceRoot}/.env" + "envFile": "${workspaceRoot}/.env", }, { "name": "Launch Webpack Dev Server", diff --git a/assets/src/js/app/embed/check.js b/assets/src/js/app/embed/check.js index e5e0dbd..734baf9 100644 --- a/assets/src/js/app/embed/check.js +++ b/assets/src/js/app/embed/check.js @@ -2,7 +2,6 @@ import message from './message'; const sha1 = require('sha1'); message.send('init'); const data = await message.receiveOnce('init'); -console.log(data); const c = await check(); if (c) { initPlaceholder(data); @@ -45,14 +44,17 @@ function initEmbed(data) { const csrf = document.createElement('input'); csrf.setAttribute('name', '_csrf'); csrf.setAttribute('value', window._CSRF); + csrf.setAttribute('type', 'hidden'); form.append(csrf); const sessionID = document.createElement('input'); sessionID.setAttribute('name', '_sessionID'); sessionID.setAttribute('value', window._sessionID); + sessionID.setAttribute('type', 'hidden'); form.append(sessionID); const i = document.createElement('input'); i.setAttribute('name', 'settings'); i.setAttribute('value', JSON.stringify(data)); + i.setAttribute('type', 'hidden'); form.append(i); document.body.append(form); // form.setAttribute('action', '/'); diff --git a/assets/src/js/app/embed/index.js b/assets/src/js/app/embed/index.js index a9d0b94..11fe92c 100644 --- a/assets/src/js/app/embed/index.js +++ b/assets/src/js/app/embed/index.js @@ -1,3 +1,5 @@ +import message from './message'; + function setHeight() { const width = document.body.offsetWidth; const height = width/16*9; @@ -15,21 +17,25 @@ window.addEventListener('load', async () => { document.body.style.height = 'auto'; progress.classList.add('hidden'); el.classList.remove('hidden'); + if (window._domainSettings.ads && window._injectAds) { + console.log('injecting ads'); + message.send('inject', window._injectAds); + } }, {once: true}); el.classList.add('hidden'); el.classList.add('mb-5') document.body.appendChild(el); ev.render(el); }); - if (!window._settings.height) { + if (!window._embedSettings.height) { const s = document.createElement('script'); s.src = 'assets/lib/iframeResizer.contentWindow.min.js'; document.body.appendChild(s); } setHeight(); window.addEventListener('resize', setHeight); - if (window._settings.poster) { - document.body.style.backgroundImage = 'url(' + window._settings.poster + ')'; + if (window._embedSettings.poster) { + document.body.style.backgroundImage = 'url(' + window._embedSettings.poster + ')'; document.body.style.backgroundSize = 'cover'; } }); \ No newline at end of file diff --git a/configure.go b/configure.go index 132e9b4..2cd9f10 100644 --- a/configure.go +++ b/configure.go @@ -2,9 +2,11 @@ package main import ( "github.com/urfave/cli" + services "github.com/webtor-io/common-services" ) func configure(app *cli.App) { serveCmd := makeServeCMD() - app.Commands = []cli.Command{serveCmd} + migrationCMD := services.MakePGMigrationCMD() + app.Commands = []cli.Command{serveCmd, migrationCMD} } diff --git a/go.mod b/go.mod index f813b6d..4b961eb 100644 --- a/go.mod +++ b/go.mod @@ -100,6 +100,7 @@ require ( github.com/prometheus/common v0.54.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/satori/go.uuid v1.2.0 github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/objx v0.5.2 // indirect diff --git a/go.sum b/go.sum index d4711ee..115b909 100644 --- a/go.sum +++ b/go.sum @@ -533,6 +533,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= diff --git a/migrations/1_embed_domain.down.sql b/migrations/1_embed_domain.down.sql new file mode 100644 index 0000000..4730dbd --- /dev/null +++ b/migrations/1_embed_domain.down.sql @@ -0,0 +1,2 @@ +DROP TABLE public.embed_domain; +DROP FUNCTION public.update_updated_at(); \ No newline at end of file diff --git a/migrations/1_embed_domain.up.sql b/migrations/1_embed_domain.up.sql new file mode 100644 index 0000000..9bb2c32 --- /dev/null +++ b/migrations/1_embed_domain.up.sql @@ -0,0 +1,28 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +CREATE TABLE public.embed_domain ( + embed_domain_id uuid DEFAULT uuid_generate_v4() NOT NULL, + "domain" text NOT NULL, + email text NOT NULL, + created_at timestamptz DEFAULT now() NOT NULL, + ads bool DEFAULT true NOT NULL, + updated_at timestamptz DEFAULT now() NOT NULL, + CONSTRAINT embed_domain_pk PRIMARY KEY (embed_domain_id), + CONSTRAINT embed_domain_unique UNIQUE (domain) +); + +CREATE OR REPLACE FUNCTION public.update_updated_at() + RETURNS trigger + LANGUAGE plpgsql +AS $function$ +BEGIN + NEW.updated_at = now(); + RETURN NEW; +END; +$function$ +; + +create trigger update_updated_at before +update + on + public.embed_domain for each row execute function update_updated_at(); \ No newline at end of file diff --git a/serve.go b/serve.go index fcaf3c3..bcfe29f 100644 --- a/serve.go +++ b/serve.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/webtor-io/web-ui-v2/services/api" "github.com/webtor-io/web-ui-v2/services/auth" + "github.com/webtor-io/web-ui-v2/services/embed" log "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -41,6 +42,7 @@ func makeServeCMD() cli.Command { } func configureServe(c *cli.Command) { + c.Flags = cs.RegisterPGFlags(c.Flags) c.Flags = cs.RegisterProbeFlags(c.Flags) c.Flags = api.RegisterFlags(c.Flags) c.Flags = w.RegisterFlags(c.Flags) @@ -55,6 +57,16 @@ func configureServe(c *cli.Command) { } func serve(c *cli.Context) error { + // Setting DB + pg := cs.NewPG(c) + defer pg.Close() + + // Setting Migrations + m := cs.NewPGMigration(pg) + err := m.Run() + if err != nil { + return err + } // Setting Redis // redis := cs.NewRedisClient(c) @@ -137,6 +149,9 @@ func serve(c *cli.Context) error { uc.RegisterHandler(c, r) } + // Setting DomainSettings + ds := embed.NewDomainSettings(pg, uc) + // Setting ApiClaimsHandler api.RegisterHandler(c, r) @@ -156,7 +171,7 @@ func serve(c *cli.Context) error { wee.RegisterHandler(c, r, tm) // Setting EmbedHandler - we.RegisterHandler(c, r, tm, jobs) + we.RegisterHandler(c, r, tm, jobs, ds) // Render templates err = tm.Init() diff --git a/services/embed/domain_settings.go b/services/embed/domain_settings.go new file mode 100644 index 0000000..0ed9126 --- /dev/null +++ b/services/embed/domain_settings.go @@ -0,0 +1,60 @@ +package embed + +import ( + "time" + + "github.com/go-pg/pg/v10" + cs "github.com/webtor-io/common-services" + "github.com/webtor-io/lazymap" + "github.com/webtor-io/web-ui-v2/services/claims" + "github.com/webtor-io/web-ui-v2/services/models" +) + +type DomainSettings struct { + lazymap.LazyMap + pg *cs.PG + claims *claims.Claims +} +type DomainSettingsData struct { + Ads bool `json:"ads"` +} + +func NewDomainSettings(pg *cs.PG, claims *claims.Claims) *DomainSettings { + return &DomainSettings{ + pg: pg, + claims: claims, + LazyMap: lazymap.New(&lazymap.Config{ + Expire: time.Minute, + ErrorExpire: 10 * time.Second, + }), + } +} + +func (s *DomainSettings) get(domain string) (*DomainSettingsData, error) { + if s.pg == nil || s.pg.Get() == nil || s.claims == nil { + return &DomainSettingsData{}, nil + } + db := s.pg.Get() + em := &models.EmbedDomain{} + err := db.Model(em).Where("domain = ?", domain).Select() + if err == pg.ErrNoRows { + return &DomainSettingsData{Ads: true}, nil + } else if err != nil { + return nil, err + } + cl, err := s.claims.Get(em.Email) + if err != nil { + return nil, err + } + return &DomainSettingsData{Ads: em.Ads || !cl.Claims.Embed.NoAds}, nil +} + +func (s *DomainSettings) Get(domain string) (*DomainSettingsData, error) { + resp, err := s.LazyMap.Get(domain, func() (interface{}, error) { + return s.get(domain) + }) + if err != nil { + return nil, err + } + return resp.(*DomainSettingsData), nil +} diff --git a/services/models/embed_domain.go b/services/models/embed_domain.go new file mode 100644 index 0000000..24dfb53 --- /dev/null +++ b/services/models/embed_domain.go @@ -0,0 +1,17 @@ +package models + +import ( + "time" + + uuid "github.com/satori/go.uuid" +) + +type EmbedDomain struct { + tableName struct{} `pg:"embed_domain"` + ID uuid.UUID `pg:"embed_domain_id,type:uuid,pk,default:uuid_generate_v4()"` + Domain string + Email string + Ads bool + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/services/web/embed/handler.go b/services/web/embed/handler.go index 1c53d8b..ead787f 100644 --- a/services/web/embed/handler.go +++ b/services/web/embed/handler.go @@ -3,6 +3,7 @@ package embed import ( "github.com/gin-gonic/gin" "github.com/urfave/cli" + "github.com/webtor-io/web-ui-v2/services/embed" "github.com/webtor-io/web-ui-v2/services/template" j "github.com/webtor-io/web-ui-v2/services/web/job" ) @@ -10,12 +11,14 @@ import ( type Handler struct { tb template.Builder jobs *j.Handler + ds *embed.DomainSettings } -func RegisterHandler(c *cli.Context, r *gin.Engine, tm *template.Manager, jobs *j.Handler) { +func RegisterHandler(c *cli.Context, r *gin.Engine, tm *template.Manager, jobs *j.Handler, ds *embed.DomainSettings) { h := &Handler{ tb: tm.MustRegisterViews("embed/*"), jobs: jobs, + ds: ds, } r.GET("/embed", h.get) r.POST("/embed", h.post) diff --git a/services/web/embed/post.go b/services/web/embed/post.go index 2da884e..fcbda3f 100644 --- a/services/web/embed/post.go +++ b/services/web/embed/post.go @@ -3,23 +3,26 @@ package embed import ( "encoding/json" "net/http" + "net/url" "github.com/gin-gonic/gin" "github.com/webtor-io/web-ui-v2/services/api" + "github.com/webtor-io/web-ui-v2/services/embed" "github.com/webtor-io/web-ui-v2/services/job" "github.com/webtor-io/web-ui-v2/services/web/job/script" ) type PostArgs struct { - ID string - Settings *script.EmbedSettings - Claims *api.Claims + ID string + EmbedSettings *script.EmbedSettings + Claims *api.Claims } type PostData struct { - ID string - Settings *script.EmbedSettings - Job *job.Job + ID string + EmbedSettings *script.EmbedSettings + DomainSettings *embed.DomainSettingsData + Job *job.Job } func (s *Handler) bindPostArgs(c *gin.Context) (*PostArgs, error) { @@ -32,9 +35,9 @@ func (s *Handler) bindPostArgs(c *gin.Context) (*PostArgs, error) { id := c.Query("id") return &PostArgs{ - ID: id, - Settings: &settings, - Claims: api.GetClaimsFromContext(c), + ID: id, + EmbedSettings: &settings, + Claims: api.GetClaimsFromContext(c), }, nil } @@ -48,8 +51,19 @@ func (s *Handler) post(c *gin.Context) { return } pd.ID = args.ID - pd.Settings = args.Settings - job, err := s.jobs.Embed(c, args.Claims, args.Settings) + u, err := url.Parse(args.EmbedSettings.Referer) + if err != nil { + tpl.HTMLWithErr(err, http.StatusBadRequest, c, pd) + return + } + dsd, err := s.ds.Get(u.Hostname()) + if err != nil { + tpl.HTMLWithErr(err, http.StatusBadRequest, c, pd) + return + } + pd.EmbedSettings = args.EmbedSettings + pd.DomainSettings = dsd + job, err := s.jobs.Embed(c, args.Claims, args.EmbedSettings) if err != nil { tpl.HTMLWithErr(err, http.StatusBadRequest, c, pd) return diff --git a/services/web/job/script/embed.go b/services/web/job/script/embed.go index 46dc71e..ddfaad8 100644 --- a/services/web/job/script/embed.go +++ b/services/web/job/script/embed.go @@ -23,6 +23,7 @@ type EmbedSettings struct { StreamSettings Version string `json:"version"` Magnet string `json:"magnet"` + Referer string `json:"referer"` } type EmbedScript struct { diff --git a/templates/layouts/embed/example.html b/templates/layouts/embed/example.html index a8756e7..af72558 100644 --- a/templates/layouts/embed/example.html +++ b/templates/layouts/embed/example.html @@ -17,6 +17,6 @@ {{ template "main" . }} - + \ No newline at end of file diff --git a/templates/partials/extend_base.html b/templates/partials/extend_base.html index 22638a2..514c812 100644 --- a/templates/partials/extend_base.html +++ b/templates/partials/extend_base.html @@ -1,4 +1,6 @@ {{ define "head_extra" }} {{ end }} {{ define "get_extra" }} +{{ end }} +{{ define "embed_extra" }} {{ end }} \ No newline at end of file diff --git a/templates/views/embed/post.html b/templates/views/embed/post.html index b311de7..3d7b017 100644 --- a/templates/views/embed/post.html +++ b/templates/views/embed/post.html @@ -4,9 +4,11 @@ + {{ template "embed_extra" . }} {{ "embed.css" | asset }} {{ "embed/index.js" | asset }} @@ -18,6 +20,8 @@ {{ end }} {{ with .Data }} + + {{ if has . "Job" }} {{ end }} + {{ end }} \ No newline at end of file