Compare commits
No commits in common. "master" and "v1.2.0" have entirely different histories.
3 changed files with 1 additions and 43 deletions
|
@ -1,12 +0,0 @@
|
||||||
defaults:
|
|
||||||
run:
|
|
||||||
shell: sh
|
|
||||||
on: [push]
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: alpine
|
|
||||||
steps:
|
|
||||||
- run: apk add nodejs-current git
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- run: apk add go
|
|
||||||
- run: go build
|
|
|
@ -9,8 +9,3 @@ Environmental variables:
|
||||||
- `SHORTEN_BIND` - bind address (default: `127.0.0.1:4488`)
|
- `SHORTEN_BIND` - bind address (default: `127.0.0.1:4488`)
|
||||||
- `SHORTEN_MAIL` - optional email for support/abuse reports
|
- `SHORTEN_MAIL` - optional email for support/abuse reports
|
||||||
- `POSTGRES_URI` - lib/pq connection string (see [here](https://pkg.go.dev/github.com/lib/pq#section-documentation))
|
- `POSTGRES_URI` - lib/pq connection string (see [here](https://pkg.go.dev/github.com/lib/pq#section-documentation))
|
||||||
|
|
||||||
### Maintenance
|
|
||||||
|
|
||||||
shorten supports a domain blacklist to ban certain domains (e.g. spam, malware, etc.);
|
|
||||||
you can use it by connecting to the database and inserting rows into the `blacklist` table.
|
|
||||||
|
|
27
main.go
27
main.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
neturl "net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -72,13 +71,6 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS blacklist (
|
|
||||||
domain text not null
|
|
||||||
);`)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bind := os.Getenv("SHORTEN_BIND")
|
bind := os.Getenv("SHORTEN_BIND")
|
||||||
if bind == "" {
|
if bind == "" {
|
||||||
bind = "127.0.0.1:4488"
|
bind = "127.0.0.1:4488"
|
||||||
|
@ -175,25 +167,8 @@ func (h *Handler) GetCode(url string, ip string) (string, error) {
|
||||||
return "", fmt.Errorf("invalid URL")
|
return "", fmt.Errorf("invalid URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
parsed, err := neturl.Parse(url)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("invalid URL")
|
|
||||||
}
|
|
||||||
hostname := parsed.Hostname()
|
|
||||||
|
|
||||||
// tbh we don't care about the value here but why not
|
|
||||||
var res string
|
|
||||||
err = h.db.QueryRow(`SELECT domain FROM blacklist WHERE domain = $1`, hostname).Scan(&res)
|
|
||||||
if err != sql.ErrNoRows {
|
|
||||||
if err != nil {
|
|
||||||
log.Println("sql error:", err)
|
|
||||||
return "", fmt.Errorf("query: %w", err)
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("invalid URL")
|
|
||||||
}
|
|
||||||
|
|
||||||
var code string
|
var code string
|
||||||
err = h.db.QueryRow(`SELECT code FROM urls WHERE url = $1 LIMIT 1`, url).Scan(&code)
|
err := h.db.QueryRow(`SELECT code FROM urls WHERE url = $1 LIMIT 1`, url).Scan(&code)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return code, nil
|
return code, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue