-
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
1 parent
7939a84
commit 2192b10
Showing
19 changed files
with
2,020 additions
and
3 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
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,130 @@ | ||
package helper | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// func PrettyPrintJSON(data []map[string]string) string { | ||
func PrettyPrintJSON(data any) string { | ||
b, err := json.MarshalIndent(data, "", " ") | ||
if err != nil { | ||
fmt.Println("error:", err) | ||
} | ||
fmt.Println(string(b)) | ||
return string(b) | ||
} | ||
|
||
/* | ||
splits a string by delimiter but ignores delimiter escaped by escape | ||
in: aaaaa:bbbb:cccc\:dddd | ||
out: [aaaaa,bbbb,cccc\:dddd] | ||
splitIgnoreEscape(line2, ':', '\\') | ||
*/ | ||
func SplitIgnoreEscape(str string, delimiter byte, escape byte) []string { | ||
var parts []string | ||
var buffer strings.Builder | ||
|
||
escaped := false | ||
|
||
for i := 0; i < len(str); i++ { | ||
char := str[i] | ||
|
||
if !escaped && char == escape { | ||
escaped = true | ||
continue | ||
} | ||
|
||
if escaped { | ||
if char != delimiter && char != escape { | ||
buffer.WriteByte(escape) | ||
} | ||
buffer.WriteByte(char) | ||
escaped = false | ||
continue | ||
} | ||
|
||
if char == delimiter { | ||
parts = append(parts, buffer.String()) | ||
buffer.Reset() | ||
continue | ||
} | ||
|
||
buffer.WriteByte(char) | ||
} | ||
|
||
parts = append(parts, buffer.String()) | ||
|
||
return parts | ||
} | ||
|
||
/* | ||
splits a string by delimiter and returns a slice of non empty strings | ||
SplitStringIgnoreEmpty(value, ";") | ||
in "aaaa;bbbb;;cccc" | ||
out ["aaaa", "bbbb", "cccc"] | ||
*/ | ||
func SplitStringIgnoreEmpty(value string, delimiter string) []string { | ||
filteredSlice := make([]string, 0) | ||
for _, str := range strings.Split(value, ";") { | ||
if strings.TrimSpace(str) != "" { | ||
filteredSlice = append(filteredSlice, str) | ||
} | ||
} | ||
return filteredSlice | ||
} | ||
|
||
// sha256 hash from map[string]string | ||
func Sha265HashFromMap(m map[string]string) string { | ||
// Get a sorted slice of the keys | ||
keys := make([]string, 0, len(m)) | ||
for key := range m { | ||
keys = append(keys, key) | ||
} | ||
sort.Strings(keys) | ||
|
||
// Concatenate the key-value pairs | ||
var str string | ||
for _, key := range keys { | ||
str += key + m[key] | ||
} | ||
|
||
// Compute the SHA-256 hash | ||
hash := sha256.Sum256([]byte(str)) | ||
|
||
// Return the hexadecimal encoding of the hash | ||
return hex.EncodeToString(hash[:]) | ||
} | ||
|
||
/* | ||
takes two []string slices as input and returns true if all the elements in the first slice (s1) are present in the second slice (s2), regardless of their order, and false otherwise. | ||
The function first checks if the two slices have the same length, and if not, it immediately returns false. Then, it creates a map (m) and adds each element of the second slice (s2) to the map as a key with a value of true. | ||
Next, it iterates over the elements of the first slice (s1) and checks if each element is present in the map (m). If any element is not present in the map, the function returns false. If all elements are present in the map, the function returns true. | ||
*/ | ||
func AreEqualSlices(s1, s2 []string) bool { | ||
if len(s1) != len(s2) { | ||
return false | ||
} | ||
|
||
m := make(map[string]bool) | ||
for _, v := range s2 { | ||
m[v] = true | ||
} | ||
|
||
for _, v := range s1 { | ||
if _, ok := m[v]; !ok { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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 epsignal | ||
|
||
import ( | ||
"github.com/ricoschulte/go-myapps/service" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type EpSignal struct { | ||
} | ||
|
||
func (api *EpSignal) GetApiName() string { | ||
return "EpSignal" | ||
} | ||
|
||
func (api *EpSignal) OnConnect(connection *service.AppServicePbxConnection) { | ||
log.WithField("api", api.GetApiName()).Warn("OnConnect not implemented ") | ||
} | ||
|
||
func (api *EpSignal) OnDisconnect(connection *service.AppServicePbxConnection) { | ||
log.WithField("api", api.GetApiName()).Warn("OnDisconnect not implemented ") | ||
} | ||
|
||
func (api *EpSignal) HandleMessage(connection *service.AppServicePbxConnection, msg *service.BaseMessage, message []byte) { | ||
log.WithField("api", api.GetApiName()).Warn("HandleMessage not implemented ") | ||
} |
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,97 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"net/http" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func AppIndex(appservice *AppService, w http.ResponseWriter, req *http.Request) { | ||
w.Header().Add("Content-Type", "text/html") | ||
w.Write([]byte("app index")) | ||
} | ||
|
||
// handles both websocket and http GET/POST requests on the same path | ||
func handleConnectionForHttpOrWebsocket(appservice *AppService, w http.ResponseWriter, r *http.Request) { | ||
log.Warnf("serve handleConnectionForHttpOrWebsocket %s", r.URL.Path) | ||
if r.URL.Path != "/test.company.com/testservice/mytestservice" { | ||
return | ||
} | ||
if r.Method != http.MethodGet && r.Method != http.MethodPost { | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
if r.Header.Get("Upgrade") == "websocket" { | ||
HandleWebsocket(appservice, w, r) | ||
} else { | ||
AppIndex(appservice, w, r) | ||
} | ||
} | ||
|
||
func ServeFile(fs http.FileSystem, w http.ResponseWriter, r *http.Request, filename string, contenttype string) { | ||
log.Warnf("ServeFile %s", filename) | ||
|
||
file, err := fs.Open(filename) | ||
if err != nil { | ||
fmt.Fprintf(w, "%v", err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
content, err_file := io.ReadAll(file) | ||
if err_file != nil { | ||
fmt.Fprintf(w, "Error while reading file: %v", err_file) | ||
http.Error(w, err_file.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Add("Content-Type", contenttype) | ||
_, err_write := w.Write(content) | ||
if err_write != nil { | ||
fmt.Fprintf(w, "Error while writing to response: %v", err_write) | ||
http.Error(w, err_write.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
} | ||
|
||
// func GetHttpRoutes(appservice *AppService, mux *chi.Mux) error { | ||
|
||
// // Serve static files on app path | ||
// // mux.Get("/static/*", | ||
// // http.StripPrefix("/test.company.com/testservice/mytestservice/static/", | ||
// // http.FileServer(appservice.Fs), | ||
// // ).ServeHTTP, | ||
// // ) | ||
|
||
// // mux.Get("/admin.htm", func(w http.ResponseWriter, r *http.Request) { | ||
// // ServeFile(appservice.Fs, w, r, "admin.htm", "text/html") | ||
// // }) | ||
// // mux.Get("/user.htm", func(w http.ResponseWriter, r *http.Request) { | ||
// // log.Warnf("serve /user.htm %s", r.URL.Path) | ||
// // ServeFile(appservice.Fs, w, r, "user.htm", "text/html") | ||
// // }) | ||
// // mux.Get("/searchapi.htm", func(w http.ResponseWriter, r *http.Request) { | ||
// // ServeFile(appservice.Fs, w, r, "searchapi.htm", "text/html") | ||
// // }) | ||
|
||
// // mux.Get("/user.png", func(w http.ResponseWriter, r *http.Request) { | ||
// // ServeFile(appservice.Fs, w, r, "user.png", "image/png") | ||
// // }) | ||
// // mux.Get("/admin.png", func(w http.ResponseWriter, r *http.Request) { | ||
// // ServeFile(appservice.Fs, w, r, "admin.png", "image/png") | ||
// // }) | ||
// // mux.Get("/app.css", func(w http.ResponseWriter, r *http.Request) { | ||
// // ServeFile(appservice.Fs, w, r, "app.css", "text/css; charset=utf-8") | ||
// // }) | ||
// // mux.Get("/app.js", func(w http.ResponseWriter, r *http.Request) { | ||
// // ServeFile(appservice.Fs, w, r, "app.js", "text/javascript; charset=utf-8") | ||
// // }) | ||
|
||
// return nil | ||
// } |
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,64 @@ | ||
package service | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type MyAppsUtils struct{} | ||
|
||
func (mu *MyAppsUtils) GetDigestHashForUserLogingToAppService(app string, domain string, sip string, guid string, dn string, info string, challenge string, password string) string { | ||
str := fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s:%s", app, domain, sip, guid, dn, info, challenge, password) | ||
bytes := sha256.Sum256([]byte(str)) | ||
return hex.EncodeToString(bytes[:]) | ||
} | ||
|
||
func (mu *MyAppsUtils) GetDigestForAppLoginFromJson(message_json, password, challenge string) (string, error) { | ||
var msgin AppLogin | ||
if err := json.Unmarshal([]byte(message_json), &msgin); err != nil { | ||
return "", err | ||
} | ||
var calculated_digest string | ||
|
||
if msgin.PbxObj != "" { | ||
// user/admin login | ||
log.Trace("GetDigestForAppLoginFromJson user/admin login") | ||
info, _ := msgin.InfoAsUserDigestString() | ||
calculated_digest = mu.GetDigestHashForUserLogingToAppService(msgin.App, msgin.Domain, msgin.Sip, msgin.Guid, msgin.Dn, info, challenge, password) | ||
} else { | ||
// pbxobj login | ||
log.Trace("GetDigestForAppLoginFromJson pbxobj login") | ||
info, _ := msgin.InfoAsPbxobjectDigestString() | ||
calculated_digest = mu.GetDigestHashForUserLogingToAppService(msgin.App, msgin.Domain, msgin.Sip, msgin.Guid, msgin.Dn, info, challenge, password) | ||
} | ||
|
||
return calculated_digest, nil | ||
} | ||
|
||
func (mu *MyAppsUtils) GetRandomHexString(n int) string { | ||
charPool := "abcdef0123456789" | ||
rand.Seed(time.Now().UnixNano()) // does | ||
b := strings.Builder{} | ||
for i := 0; i < n; i++ { | ||
b.WriteByte(charPool[rand.Intn(len(charPool))]) | ||
} | ||
return b.String() | ||
} | ||
|
||
func CheckAppPasswordForMaximumLength(password string) error { | ||
if password == "" { | ||
return errors.Errorf("Error: App password cant be empty") | ||
} | ||
if len(password) > 15 { | ||
return errors.Errorf("Error: App password invalid. The app password cant be longer than 15 chars. This is a limitation of passwords stored in pbx-objects. the currently set password has a length of %v", len(password)) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.