-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ashk123
committed
Feb 5, 2024
1 parent
8f51f9f
commit a66f5e2
Showing
19 changed files
with
238 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
### How to use this folder | ||
|
||
Welcome to the config folder, in this folder you can config your chat server with the Config cmds. | ||
|
||
Be notice that the main Config file for your server is .env file you can find a sample .env file in the root of this project | ||
|
||
For Create your configuration file, just make a `config.json` file here | ||
|
||
### CMDS (JSON) | ||
|
||
_ServerName -> information: your server name <br/> | ||
Server_Description -> information: your server description<br/> | ||
Server_Owner -> information: your server manager <br/> | ||
Server_Date_Format -> Full Date format or simple Date Format_ | ||
|
||
### Example | ||
|
||
```json | ||
{ | ||
"Server_Name": "Sunsend1", | ||
"Server_Description": "This is the Default Server", | ||
"Server_Owner": "SunSend", | ||
"Sever_Date_Format": "full" // full/normal/simple | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"github.com/thanhpk/randstr" | ||
) | ||
|
||
func GenerateAPIKey() [32]byte { | ||
token := randstr.Hex(16) // generate 128-bit hex string | ||
return sha256.Sum256([]byte(token)) | ||
} | ||
|
||
func main() { | ||
res := GenerateAPIKey() | ||
fmt.Println("Your API KEY is: ", fmt.Sprintf("%X", res)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package Base | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/subtle" | ||
"fmt" | ||
"strings" | ||
"sunsend/internals/CoreConfig" | ||
) | ||
|
||
// Doc: https://dev.to/caiorcferreira/implementing-a-safe-and-sound-api-key-authorization-middleware-in-go-3g2c | ||
|
||
func GenerateAPIKey(rawkey string) [32]byte { | ||
// token := randstr.Hex(16) // generate 128-bit hex string | ||
return sha256.Sum256([]byte(rawkey)) | ||
} | ||
|
||
func BearerToken(headers map[string][]string) (string, int) { | ||
fmt.Println(headers) | ||
api_key_org, ok := headers["Api_key"] | ||
if !ok { | ||
return "", 17 // invalid API KE Y | ||
} | ||
if len(strings.SplitN(api_key_org[0], " ", 2)) > 2 { | ||
return "", 17 | ||
} | ||
return strings.TrimSpace(api_key_org[0]), 0 | ||
|
||
} | ||
|
||
// apiKeyIsValid checks if the given API key is valid and returns the principal if it is. | ||
func ApiKeyIsValid(user_api_key string) int { | ||
// hash := sha256.Sum256([]byte(user_api_key)) | ||
// key := hash[:] | ||
if subtle.ConstantTimeCompare([]byte(CoreConfig.Configs.Server.Key), []byte(user_api_key)) == 1 { | ||
return 0 | ||
} | ||
|
||
return 17 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package CoreConfig | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"os" | ||
"sunsend/internals/Data" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
var Configs *Data.Config | ||
var rawapikey string | ||
|
||
func getEnvConfig() (map[string]string, error) { | ||
err := godotenv.Load(".env") | ||
if err != nil { | ||
// log.Fatal(err.Error()) | ||
return nil, errors.New("invalid or can't read .env file") | ||
} | ||
// It can be better to list of all the needed Config and iterate thought them | ||
ret_values := make(map[string]string) | ||
ret_values["PORT"] = os.Getenv("PORT") | ||
ret_values["KEY"] = os.Getenv("KEY") | ||
// copy(rawapikey[:], ret_values["KEY"]) // fix fixing some invalid memory address - TODO: Fix in better way - nice | ||
rawapikey = ret_values["KEY"] | ||
return ret_values, nil | ||
} | ||
|
||
func UpdateConfigs() { | ||
envconfigs, err := getEnvConfig() | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
// TODO: get the user configs here | ||
|
||
Configs = &Data.Config{ | ||
Dotenv: envconfigs, | ||
Uconfig: nil, // for now just a little cute nil ^^ | ||
Server: &Data.Server{ // TODO: will holds data from user config file | ||
Name: "test", | ||
Description: "test1", | ||
Owner: "test", | ||
Date: "test", | ||
Key: rawapikey, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package Data | ||
|
||
type Config struct { | ||
Dotenv map[string]string // .env file configs | ||
Uconfig map[string]string // usr `Config` folder configs | ||
Server *Server | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package Data | ||
|
||
type Server struct { | ||
Name string | ||
Description string | ||
Owner string | ||
Date string | ||
Key string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package Data | ||
|
||
/* | ||
This file will holds the words that shouldn't be on chat or send as a data to the server | ||
It is better to not change edit this file and instead make a words list on config/words.l | ||
There words are default for each server, and you should not remove these. | ||
*/ | ||
|
||
var LoadedWordList *[]string | ||
|
||
var defaultWords []string = []string{ | ||
"321", | ||
"nice", | ||
"yes", | ||
} | ||
|
||
func LoadWordsFromConfig() { | ||
// Load Words From Config File | ||
LoadedWordList = &[]string{"config"} | ||
*LoadedWordList = append(*LoadedWordList, defaultWords...) | ||
} | ||
|
||
func GetAllWords() *[]string { | ||
return LoadedWordList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters