aboutsummaryrefslogtreecommitdiff
path: root/post/tripcode.go
diff options
context:
space:
mode:
authorFChannel <>2022-05-08 14:57:40 -0700
committerFChannel <>2022-06-19 12:53:29 -0700
commit580dec5b89215310ce34341e11ff17fe38bdb63a (patch)
tree894424df66a9d9f7e41805822f29adac8fb490fe /post/tripcode.go
parentf7bf818d29393ceaccf4d2906557351fa6a4f49f (diff)
more cleanup, logging and error logging everywhere
things are mostly in place can work on "features" and polish
Diffstat (limited to 'post/tripcode.go')
-rw-r--r--post/tripcode.go106
1 files changed, 50 insertions, 56 deletions
diff --git a/post/tripcode.go b/post/tripcode.go
index 3b7e48b..d5ae2e2 100644
--- a/post/tripcode.go
+++ b/post/tripcode.go
@@ -6,6 +6,7 @@ import (
"strings"
"github.com/FChannel0/FChannel-Server/config"
+ "github.com/FChannel0/FChannel-Server/util"
"github.com/gofiber/fiber/v2"
_ "github.com/lib/pq"
"github.com/simia-tech/crypt"
@@ -23,34 +24,63 @@ const SaltTable = "" +
"................................" +
"................................"
-func TripCode(pass string) (string, error) {
- pass = TripCodeConvert(pass)
+func CreateNameTripCode(ctx *fiber.Ctx) (string, string, error) {
+ input := ctx.FormValue("name")
+ tripSecure := regexp.MustCompile("##(.+)?")
- var salt [2]rune
+ if tripSecure.MatchString(input) {
+ chunck := tripSecure.FindString(input)
+ chunck = strings.Replace(chunck, "##", "", 1)
+ ce := regexp.MustCompile(`(?i)Admin`)
+ admin := ce.MatchString(chunck)
+ board, modcred := util.GetPasswordFromSession(ctx)
- s := []rune(pass + "H..")[1:3]
+ if hasAuth, _ := util.HasAuth(modcred, board); hasAuth && admin {
+ return tripSecure.ReplaceAllString(input, ""), "#Admin", nil
+ }
- for i, r := range s {
- salt[i] = rune(SaltTable[r%256])
+ hash, err := TripCodeSecure(chunck)
+
+ return tripSecure.ReplaceAllString(input, ""), "!!" + hash, util.MakeError(err, "CreateNameTripCode")
}
- enc, err := crypt.Crypt(pass, "$1$"+string(salt[:]))
- if err != nil {
- return "", err
+ trip := regexp.MustCompile("#(.+)?")
+
+ if trip.MatchString(input) {
+ chunck := trip.FindString(input)
+ chunck = strings.Replace(chunck, "#", "", 1)
+ ce := regexp.MustCompile(`(?i)Admin`)
+ admin := ce.MatchString(chunck)
+ board, modcred := util.GetPasswordFromSession(ctx)
+
+ if hasAuth, _ := util.HasAuth(modcred, board); hasAuth && admin {
+ return trip.ReplaceAllString(input, ""), "#Admin", nil
+ }
+
+ hash, err := TripCode(chunck)
+ return trip.ReplaceAllString(input, ""), "!" + hash, util.MakeError(err, "CreateNameTripCode")
}
- // normally i would just return error here but if the encrypt fails, this operation may fail and as a result cause a panic
- return enc[len(enc)-10 : len(enc)], nil
+ return input, "", nil
}
-func TripCodeSecure(pass string) (string, error) {
+func TripCode(pass string) (string, error) {
+ var salt [2]rune
+
pass = TripCodeConvert(pass)
+ s := []rune(pass + "H..")[1:3]
+
+ for i, r := range s {
+ salt[i] = rune(SaltTable[r%256])
+ }
+
+ enc, err := crypt.Crypt(pass, "$1$"+string(salt[:]))
- enc, err := crypt.Crypt(pass, "$1$"+config.Salt)
if err != nil {
- return "", err
+ return "", util.MakeError(err, "TripCode")
}
+ // normally i would just return error here but if the encrypt fails, this operation may fail and as a result cause a panic
return enc[len(enc)-10 : len(enc)], nil
}
@@ -58,7 +88,6 @@ func TripCodeConvert(str string) string {
var s bytes.Buffer
transform.NewWriter(&s, japanese.ShiftJIS.NewEncoder()).Write([]byte(str))
-
re := strings.NewReplacer(
"&", "&amp;",
"\"", "&quot;",
@@ -69,48 +98,13 @@ func TripCodeConvert(str string) string {
return re.Replace(s.String())
}
-func CreateNameTripCode(ctx *fiber.Ctx) (string, string, error) {
- // TODO: to allow this to compile, this will fail for the case of the admin
- // this can be easily fixed when the rest of the code gets converted to fiber
-
- input := ctx.FormValue("name")
-
- tripSecure := regexp.MustCompile("##(.+)?")
-
- if tripSecure.MatchString(input) {
- chunck := tripSecure.FindString(input)
- chunck = strings.Replace(chunck, "##", "", 1)
-
- //ce := regexp.MustCompile(`(?i)Admin`)
- //admin := ce.MatchString(chunck)
-
- //board, modcred := GetPasswordFromSession(r)
-
- //if admin && HasAuth(modcred, board) {
- // return tripSecure.ReplaceAllString(input, ""), "#Admin"
- //}
-
- hash, err := TripCodeSecure(chunck)
- return tripSecure.ReplaceAllString(input, ""), "!!" + hash, err
- }
-
- trip := regexp.MustCompile("#(.+)?")
-
- if trip.MatchString(input) {
- chunck := trip.FindString(input)
- chunck = strings.Replace(chunck, "#", "", 1)
-
- //ce := regexp.MustCompile(`(?i)Admin`)
- //admin := ce.MatchString(chunck)
- //board, modcred := GetPasswordFromSession(r)
-
- //if admin && HasAuth(db, modcred, board) {
- // return trip.ReplaceAllString(input, ""), "#Admin"
- //}
+func TripCodeSecure(pass string) (string, error) {
+ pass = TripCodeConvert(pass)
+ enc, err := crypt.Crypt(pass, "$1$"+config.Salt)
- hash, err := TripCode(chunck)
- return trip.ReplaceAllString(input, ""), "!" + hash, err
+ if err != nil {
+ return "", util.MakeError(err, "TripCodeSecure")
}
- return input, "", nil
+ return enc[len(enc)-10 : len(enc)], nil
}