baum
This commit is contained in:
39
src/main.go
39
src/main.go
@ -22,7 +22,10 @@ import (
|
|||||||
)
|
)
|
||||||
var discord *discordgo.Session
|
var discord *discordgo.Session
|
||||||
var secret secrets_json
|
var secret secrets_json
|
||||||
|
var config config_json
|
||||||
var cacheAccounts hashmap.HashMap
|
var cacheAccounts hashmap.HashMap
|
||||||
|
var db *sql.DB
|
||||||
|
var giteaClient *gitea.Client
|
||||||
type account struct {
|
type account struct {
|
||||||
email string
|
email string
|
||||||
username string
|
username string
|
||||||
@ -55,10 +58,11 @@ type secrets_json struct {
|
|||||||
MoodleToken string `json:"moodleToken"`
|
MoodleToken string `json:"moodleToken"`
|
||||||
GiteaToken string `json:"giteaToken"`
|
GiteaToken string `json:"giteaToken"`
|
||||||
}
|
}
|
||||||
|
type config_json struct {
|
||||||
|
CreateGiteaAccount bool `json:"createGiteaAccount"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var newRbuMember *discordgo.Member
|
|
||||||
var dmChannel *discordgo.Channel
|
|
||||||
var err error
|
var err error
|
||||||
var jsonfile *os.File
|
var jsonfile *os.File
|
||||||
jsonfile, err = os.Open("secrets.json")
|
jsonfile, err = os.Open("secrets.json")
|
||||||
@ -69,23 +73,28 @@ func main() {
|
|||||||
err = json.Unmarshal(jsondata, &secret)
|
err = json.Unmarshal(jsondata, &secret)
|
||||||
log(err)
|
log(err)
|
||||||
jsonfile.Close()
|
jsonfile.Close()
|
||||||
|
jsonfile, err = os.Open("config.json")
|
||||||
|
log(err)
|
||||||
|
jsondata, err = ioutil.ReadAll(jsonfile)
|
||||||
|
log(err)
|
||||||
|
err = json.Unmarshal(jsondata, &config)
|
||||||
discordgo.MakeIntent(discordgo.IntentsAll)
|
discordgo.MakeIntent(discordgo.IntentsAll)
|
||||||
discord, err = discordgo.New("Bot " + secret.DiscordToken)
|
discord, err = discordgo.New("Bot " + secret.DiscordToken)
|
||||||
log(err)
|
log(err)
|
||||||
err = discord.Open()
|
err = discord.Open()
|
||||||
log(err)
|
log(err)
|
||||||
db, err := sql.Open("mysql", secret.MysqlIndentify)
|
db, err = sql.Open("mysql", secret.MysqlIndentify)
|
||||||
log(err)
|
log(err)
|
||||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS account(" +
|
_, err = db.Exec("CREATE TABLE IF NOT EXISTS account(" +
|
||||||
"username varchar(40) NOT NULL, " +
|
"username varchar(40) NOT NULL, " +
|
||||||
"email varchar(255) NOT NULL, " +
|
"email varchar(255) NOT NULL, " +
|
||||||
"hash TINYBLOB NOT NULL, " +
|
"hash TINYBLOB NOT NULL, " +
|
||||||
"salt TINYBLOB NOT NULL, " +
|
"salt TINYBLOB NOT NULL, " +
|
||||||
"discordUsername varchar(32) NOT NULL, " +
|
"discordUserId varchar(32) NOT NULL, " +
|
||||||
"PRIMARY KEY ( username )" +
|
"PRIMARY KEY ( username )" +
|
||||||
");")
|
");")
|
||||||
log(err)
|
log(err)
|
||||||
giteaClient, err := gitea.NewClient("https://git.redstoneunion.de", gitea.SetToken(secret.GiteaToken))
|
giteaClient, err = gitea.NewClient("https://git.redstoneunion.de", gitea.SetToken(secret.GiteaToken))
|
||||||
log(err)
|
log(err)
|
||||||
moodle := moodle.NewMoodleApi("https://exam.redstoneunion.de/", secret.MoodleToken)
|
moodle := moodle.NewMoodleApi("https://exam.redstoneunion.de/", secret.MoodleToken)
|
||||||
_ = moodle
|
_ = moodle
|
||||||
@ -94,23 +103,24 @@ func main() {
|
|||||||
remail := regexp2.MustCompile("^(?=.{0,255}$)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$", 0)
|
remail := regexp2.MustCompile("^(?=.{0,255}$)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$", 0)
|
||||||
rusername := regexp.MustCompile("^([[:lower:]]|\\d|_|-|\\.){1,40}$")
|
rusername := regexp.MustCompile("^([[:lower:]]|\\d|_|-|\\.){1,40}$")
|
||||||
rpassword := regexp2.MustCompile("^(?=.{8,255}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\\W).*$", 0)
|
rpassword := regexp2.MustCompile("^(?=.{8,255}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\\W).*$", 0)
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
|
||||||
registerstruct := registertmpl{}
|
registerstruct := registertmpl{}
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
var newAccount account
|
var newAccount account
|
||||||
|
var newRbuMember *discordgo.Member
|
||||||
var split = strings.Split(r.FormValue("discordUser"), "#")
|
var split = strings.Split(r.FormValue("discordUser"), "#")
|
||||||
if len(split) == 2 {
|
|
||||||
newAccount = account{
|
newAccount = account{
|
||||||
email: r.FormValue("email"),
|
email: r.FormValue("email"),
|
||||||
username: r.FormValue("username"),
|
username: r.FormValue("username"),
|
||||||
password: r.FormValue("password"),
|
password: r.FormValue("password"),
|
||||||
discordUsername: split[0],
|
|
||||||
discordTag: split[1],
|
|
||||||
}
|
}
|
||||||
|
if len(split) == 2 {
|
||||||
|
newAccount.discordUsername = split[0]
|
||||||
|
newAccount.discordTag = split[1]
|
||||||
}
|
}
|
||||||
registerstruct.WrongAccount.Email, _ = remail.MatchString(newAccount.email)
|
registerstruct.WrongAccount.Email, _ = remail.MatchString(newAccount.email)
|
||||||
registerstruct.WrongAccount.Email = !registerstruct.WrongAccount.Email
|
registerstruct.WrongAccount.Email = !registerstruct.WrongAccount.Email
|
||||||
registerstruct.WrongAccount.User = !rusername.MatchString(newAccount.username) || strings.Contains(newAccount.username, "\"")
|
registerstruct.WrongAccount.User = !rusername.MatchString(newAccount.username)
|
||||||
registerstruct.WrongAccount.Pass, _ = rpassword.MatchString(newAccount.password)
|
registerstruct.WrongAccount.Pass, _ = rpassword.MatchString(newAccount.password)
|
||||||
registerstruct.WrongAccount.Pass = !registerstruct.WrongAccount.Pass
|
registerstruct.WrongAccount.Pass = !registerstruct.WrongAccount.Pass
|
||||||
newRbuMember, registerstruct.WrongAccount.DiscordUser = getRbuMember(newAccount.discordUsername, newAccount.discordTag)
|
newRbuMember, registerstruct.WrongAccount.DiscordUser = getRbuMember(newAccount.discordUsername, newAccount.discordTag)
|
||||||
@ -122,7 +132,7 @@ func main() {
|
|||||||
{
|
{
|
||||||
var username string
|
var username string
|
||||||
registerstruct.AlreadyEsitsInDatabase.Username = db.QueryRow("select username from account where username = ?", newAccount.username).Scan(&username) == nil || UsernameExistsInMem(newAccount.username) // check if username exits
|
registerstruct.AlreadyEsitsInDatabase.Username = db.QueryRow("select username from account where username = ?", newAccount.username).Scan(&username) == nil || UsernameExistsInMem(newAccount.username) // check if username exits
|
||||||
registerstruct.AlreadyEsitsInDatabase.DiscordUsername = db.QueryRow("select username from account where discordUsername = ?", newAccount.discordUsername).Scan(&username) == nil || discordUsernameExistsInMem(newAccount.discordId)
|
registerstruct.AlreadyEsitsInDatabase.DiscordUsername = db.QueryRow("select username from account where discordUserId = ?", newAccount.discordId).Scan(&username) == nil || discordUsernameExistsInMem(newAccount.discordId)
|
||||||
}
|
}
|
||||||
registerstruct.Success = !registerstruct.WrongAccount.User && !registerstruct.WrongAccount.Pass && !registerstruct.WrongAccount.Email && !registerstruct.WrongAccount.DiscordUser && !registerstruct.AlreadyEsitsInDatabase.DiscordUsername && !registerstruct.AlreadyEsitsInDatabase.Username
|
registerstruct.Success = !registerstruct.WrongAccount.User && !registerstruct.WrongAccount.Pass && !registerstruct.WrongAccount.Email && !registerstruct.WrongAccount.DiscordUser && !registerstruct.AlreadyEsitsInDatabase.DiscordUsername && !registerstruct.AlreadyEsitsInDatabase.Username
|
||||||
if !registerstruct.Success {
|
if !registerstruct.Success {
|
||||||
@ -130,6 +140,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
token, err := GenerateRandomStringURLSafe(64)
|
token, err := GenerateRandomStringURLSafe(64)
|
||||||
log(err)
|
log(err)
|
||||||
|
var dmChannel *discordgo.Channel
|
||||||
dmChannel, err = discord.UserChannelCreate(newRbuMember.User.ID)
|
dmChannel, err = discord.UserChannelCreate(newRbuMember.User.ID)
|
||||||
log(err)
|
log(err)
|
||||||
discord.ChannelMessageSend(dmChannel.ID, "Bitte klicke auf den Link, um die Erstellung des Accounts abzuschließen.\nhttp://localhost:8080/submit?token=" + token)
|
discord.ChannelMessageSend(dmChannel.ID, "Bitte klicke auf den Link, um die Erstellung des Accounts abzuschließen.\nhttp://localhost:8080/submit?token=" + token)
|
||||||
@ -154,16 +165,17 @@ func main() {
|
|||||||
log(err)
|
log(err)
|
||||||
hash := argon2.IDKey([]byte(account.password), salt, 1, 64*1024, 4, 32)
|
hash := argon2.IDKey([]byte(account.password), salt, 1, 64*1024, 4, 32)
|
||||||
// add user to the database
|
// add user to the database
|
||||||
query := "INSERT INTO account(username, email, hash, salt, discordUsername) VALUES (?, ?, ?, ?, ?)"
|
query := "INSERT INTO account(username, email, hash, salt, discordUserId) VALUES (?, ?, ?, ?, ?)"
|
||||||
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancelfunc()
|
defer cancelfunc()
|
||||||
stmt, err := db.PrepareContext(ctx, query)
|
stmt, err := db.PrepareContext(ctx, query)
|
||||||
log(err)
|
log(err)
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
_, err = stmt.ExecContext(ctx, account.username, account.email, hash, salt, account.discordUsername)
|
_, err = stmt.ExecContext(ctx, account.username, account.email, hash, salt, account.discordId)
|
||||||
log(err)
|
log(err)
|
||||||
//_, err = moodle.AddUser(account.username + "wg", account.username, account.email, account.username, account.password)
|
//_, err = moodle.AddUser(account.username + "wg", account.username, account.email, account.username, account.password)
|
||||||
log(err)
|
log(err)
|
||||||
|
if config.CreateGiteaAccount {
|
||||||
opt := gitea.CreateUserOption{
|
opt := gitea.CreateUserOption{
|
||||||
Email: account.email,
|
Email: account.email,
|
||||||
Username: account.username,
|
Username: account.username,
|
||||||
@ -174,6 +186,7 @@ func main() {
|
|||||||
_, _, err = giteaClient.AdminCreateUser(opt)
|
_, _, err = giteaClient.AdminCreateUser(opt)
|
||||||
log(err)
|
log(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
submitReturn: err = submitTmpl.Execute(w, submitStruct)
|
submitReturn: err = submitTmpl.Execute(w, submitStruct)
|
||||||
log(err)
|
log(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user