Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin
log.txt
docker_root
andrew-build.sh
gatesentry-linux
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM ubuntu:latest
USER root
RUN apt-get update && apt-get install -y \
lsof \
tzdata \
net-tools \
dnsutils \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/local/gatesentry
COPY gatesentry-linux /usr/local/gatesentry
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /usr/local/gatesentry/gatesentry-linux
EXPOSE 80 53 53/UDP 10413 10786
ENTRYPOINT ["/entrypoint.sh"]
8 changes: 5 additions & 3 deletions application/filters.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package gatesentryf

import (
"context"

gatesentry2responder "bitbucket.org/abdullah_irfan/gatesentryf/responder"
"gopkg.in/elazarl/goproxy.v1"
// "strings"
)

func RunFilter(filterType string, content string, responder *gatesentry2responder.GSFilterResponder) {
func RunFilter(ctx context.Context, filterType string, content string, responder *gatesentry2responder.GSFilterResponder) {
for _, v := range R.Filters {
v.Handle(content, filterType, responder)
v.Handle(ctx, content, filterType, responder)
}
}

var ConditionalMitm goproxy.FuncHttpsHandler = func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
responder := &gatesentry2responder.GSFilterResponder{Blocked: false}

RunFilter("url/https_dontbump", host, responder)
RunFilter(context.Background(), "url/https_dontbump", host, responder)
if responder.Blocked {

// A blocked here means the url is present in the list
Expand Down
3 changes: 1 addition & 2 deletions application/filters/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions application/filters/filter-blocked-mimes.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package gatesentry2filters

import (
"context"
"strings"

gatesentry2responder "bitbucket.org/abdullah_irfan/gatesentryf/responder"
)

func FilterBlockedMimes(f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {

func FilterBlockedMimes(ctx context.Context, f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
for _, v := range f.FileContents {
// fmt.Println( v )
// log.Println( "Running for = " + v.Content + " against = " + content )
// Check if the context is canceled or timed out
select {
case <-ctx.Done():
// Exit early if the context is canceled
return
default:
// Continue processing
}

// Check if the content contains the blocked MIME type
if strings.Contains(content, v.Content) {
responder.Blocked = true
return // Exit early since the content is blocked
}
}
}
4 changes: 2 additions & 2 deletions application/filters/filter-time.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func RunTimeFilter(responder *gatesentry2responder.GSFilterResponder, blockedtim

loc, erro := time.LoadLocation(timezone)
if erro != nil {
log.Println("Location not found = " + erro.Error())
log.Printf("error loading location '%s': %v\n", timezone, erro)
return
}
log.Println("Location found in db = " + timezone)
// log.Println("Location found in db = " + timezone)
t = t.In(loc)
blocktimes := GSBlockTimes{}
// fmt.Println(blockedtimes)
Expand Down
15 changes: 13 additions & 2 deletions application/filters/filter-url-blockedhosts.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package gatesentry2filters

import (
"context"
"strings"

gatesentry2responder "bitbucket.org/abdullah_irfan/gatesentryf/responder"
)

func FilterUrlBlockedHosts(f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {

func FilterUrlBlockedHosts(ctx context.Context, f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
for _, v := range f.FileContents {
// Check if the context is canceled or timed out
select {
case <-ctx.Done():
// Exit early if the context is canceled
return
default:
// Continue processing
}

// Check if the content contains the blocked URL
if strings.Contains(content, v.Content) {
responder.Blocked = true
return // Exit early since the content is blocked
}
}
}
13 changes: 10 additions & 3 deletions application/filters/filter-url-dontbump.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package gatesentry2filters

import (
"context"
"log"

gatesentry2responder "bitbucket.org/abdullah_irfan/gatesentryf/responder"
)

func FilterUrlDontBump(f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
func FilterUrlDontBump(ctx context.Context, f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
for _, v := range f.FileContents {
// The url from the filter is in the form of url:443
// For example for https://slack.com it is slack.com:443
select {
case <-ctx.Done():
log.Println("FilterUrlDontBump operation canceled or timed out")
return
default:
// Continue processing
}

log.Println("Comparing ", content, " against internal = ", v.Content)
if content == v.Content+":443" || content == v.Content {
log.Println("URL found in list = " + v.Content)
Expand Down
13 changes: 11 additions & 2 deletions application/filters/filter-url-exceptionurls.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package gatesentry2filters

import (
"context"
"log"
"strings"

gatesentry2responder "bitbucket.org/abdullah_irfan/gatesentryf/responder"
)

func FilterUrlExceptionUrls(f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
func FilterUrlExceptionUrls(ctx context.Context, f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
log.Println("Exception filter running for = " + content)
for _, v := range f.FileContents {
select {
case <-ctx.Done():
log.Println("FilterUrlExceptionUrls operation canceled or timed out")
return
default:
// Continue processing
}

log.Println("Comparing ", content, " against ", v.Content)
if strings.Contains(v.Content, content) || strings.Contains(content, v.Content) || strings.Contains(content+":443", v.Content) {
responder.Blocked = true
responder.SetBlocked(true)
}
}
}
88 changes: 62 additions & 26 deletions application/filters/filter-words.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
package gatesentry2filters

import (
"context"
"fmt"
"log"
"strconv"
"strings"
"regexp"

gatesentry2responder "bitbucket.org/abdullah_irfan/gatesentryf/responder"
)

func FilterWords(f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {

func FilterWords(ctx context.Context, f *GSFilter, content string, responder *gatesentry2responder.GSFilterResponder) {
ReasonsForBlocking := []string{}
pts := 0
// fmt.Println( pts );
for _, v := range f.FileContents {
// fmt.Println( )
found := strings.Count(strings.ToLower(content), strings.ToLower(v.Content))
pts += found * v.Score
// fmt.Println("Found " + v.Content + " times = " + strconv.Itoa( found ));
if found > 0 {

ReasonsForBlocking = append(ReasonsForBlocking, "Found <u>"+v.Content+"</u> "+strconv.Itoa(found)+" times, weightage of each time = "+strconv.Itoa(v.Score))
// fmt.Println("Found " + v.Content + " " + strconv.Itoa(pts) + " times ");
}

// fmt.Println( "Total score = " + strconv.Itoa(pts) );
if pts > f.Strictness {
responder.Score = pts
responder.Blocked = true
responder.Reasons = ReasonsForBlocking
// Create a channel to signal completion
done := make(chan struct{})
defer close(done)

// Use a goroutine to handle the filtering logic
go func() {
// Iterate over all filter content
for _, v := range f.FileContents {
// Check if the context is canceled or expired
select {
case <-ctx.Done():
log.Println("FilterWords operation canceled or timed out")
return
default:
// Continue processing
}

// Compile regex to match the word with word boundaries on both sides
re, err := regexp.Compile(`(?i)\b` + regexp.QuoteMeta(v.Content) + `\b`)
if err != nil {
log.Printf("Invalid regex pattern: %v\n", err)
continue
}

// Find all matches in the content
matches := re.FindAllString(content, -1)

// Count matches and update points
found := len(matches)
pts += found * v.Score

// If the word is found, log the reason
if found > 0 {
reason := fmt.Sprintf("Found <u>%s</u> %d times, weightage of each time = %d <!-- %s --->", "blocked word", found, v.Score, v.Content)
ReasonsForBlocking = append(ReasonsForBlocking, reason)
}

// If total score exceeds strictness, set responder and exit
if pts > f.Strictness {
responder.Score = pts
responder.Blocked = true
responder.Reasons = ReasonsForBlocking
log.Println("Blocking content due to score threshold breach. Score:", pts)
done <- struct{}{} // Signal completion
return
}
}
}

log.Println("Score = " + strconv.Itoa(pts) + " strictness = " + strconv.Itoa(f.Strictness))
// If the loop completes, set responder to not blocked
log.Println("Final Score:", pts, "Strictness:", f.Strictness)
done <- struct{}{} // Signal completion
}()

// Wait for completion or context cancellation
select {
case <-done:
// Completed successfully
case <-ctx.Done():
// Context canceled or timed out
log.Println("FilterWords operation canceled or timed out")
}
}

// func loadfilter(){
// gatesentry2.NewGSFilter("text/html", "filterfiles/stopwords.json")
// }
24 changes: 22 additions & 2 deletions application/filters/filter-youtube.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package gatesentry2filters

import (
"context"
"fmt"
"log"
"strings"
)

func FilterYoutube() {
func FilterYoutube(ctx context.Context, url1 string) {
select {
case <-ctx.Done():
log.Println("FilterYoutube operation canceled or timed out")
return
default:
// Continue processing
}

url1 := "mainvideo url"
// Extract video ID
parts := strings.Split(url1, "/")
if len(parts) < 5 {
log.Println("Invalid URL format")
return
}
videoID := parts[4]

// Extract sqp parameter
sqpIndex := strings.Index(url1, "sqp=")
if sqpIndex == -1 {
log.Println("sqp parameter not found in URL")
return
}
sqpEndIndex := strings.Index(url1[sqpIndex:], "|48")
if sqpEndIndex == -1 {
sqpEndIndex = len(url1) - sqpIndex
Expand All @@ -24,6 +40,10 @@ func FilterYoutube() {

// Extract sigh parameter
sighIndex := strings.LastIndex(url1, "rs$")
if sighIndex == -1 {
log.Println("sigh parameter not found in URL")
return
}
sigh := url1[sighIndex:]

// Construct the new URL
Expand Down
Loading
Loading