Skip to content

Commit

Permalink
Add parse site_root
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Jul 24, 2024
1 parent b4578da commit 8267b84
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 66 deletions.
19 changes: 16 additions & 3 deletions common/seaf-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *
seafile_session_get_tmp_file_path (SeafileSession *session,
Expand Down Expand Up @@ -387,7 +388,6 @@ load_seahub_private_key (SeafileSession *session, const char *conf_dir)
{
char *conf_path = g_build_filename(conf_dir, "seahub_settings.py", NULL);
char *data = NULL;
gsize tmp_len;
GRegex *regex = NULL;
GError *error = NULL;

Expand All @@ -406,15 +406,18 @@ load_seahub_private_key (SeafileSession *session, const char *conf_dir)

char line[256];
char *key, *value, *saveptr;
char *site_root = NULL;
while (fgets(line, sizeof(line), file)) {
char *p;
key = strtok_r(line, "=", &saveptr);
value = strtok_r(NULL, "\n", &saveptr);

// Trim space of the start
for(p = key;p != '\0';p++) {
for(p = key;p && *p != '\0';p++) {
if (isspace(*p)) {
key++;
} else {
break;
}
}

Expand All @@ -423,12 +426,22 @@ load_seahub_private_key (SeafileSession *session, const char *conf_dir)
if (g_regex_match (regex, value, 0, &match_info)) {
char *sk = g_match_info_fetch (match_info, 1);
session->seahub_pk = sk;
break;
}
}
if (key && value && strncmp(key, "SITE_ROOT", 9) == 0) {
GMatchInfo *match_info;
if (g_regex_match (regex, value, 0, &match_info)) {
site_root = g_match_info_fetch (match_info, 1);
}
}
}

if (session->seahub_pk) {
if (site_root) {
session->seahub_url = g_strdup_printf("http://127.0.0.1:8000%sapi/v2.1/internal/user-list/", site_root);
} else {
session->seahub_url = g_strdup("http://127.0.0.1:8000/api/v2.1/internal/user-list/");
}
session->seahub_conn_pool = connection_pool_new ();
}

Expand Down
83 changes: 28 additions & 55 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
package main

import (
"bufio"
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -44,7 +42,8 @@ var pidFilePath string
var logFp *os.File

var dbType string
var seafileDB, ccnetDB, seahubDB *sql.DB
var seafileDB, ccnetDB *sql.DB
var seahubURL, seahubPK string

// when SQLite is used, user and group db are separated.
var userDB, groupDB *sql.DB
Expand Down Expand Up @@ -272,66 +271,40 @@ func loadSeafileDB() {
dbType = dbEngine
}

func loadSeahubDB() {
scriptPath, err := exec.LookPath("parse_seahub_db.py")
if err != nil {
log.Warnf("Failed to find script of parse_seahub_db.py: %v", err)
return
}
cmd := exec.Command("python3", scriptPath)
dbData, err := cmd.CombinedOutput()
if err != nil {
log.Warnf("Failed to run python parse_seahub_db.py: %v", err)
return
}
func loadSeahubPK() {
confPath := filepath.Join(centralDir, "seahub_settings.py")

dbConfig := make(map[string]string)

err = json.Unmarshal(dbData, &dbConfig)
file, err := os.Open(confPath)
if err != nil {
log.Warnf("Failed to decode seahub database json file: %v", err)
log.Warnf("Failed to open seahub_settings.py: %v", err)
return
}
defer file.Close()

dbEngine := dbConfig["ENGINE"]
dbName := dbConfig["NAME"]
user := dbConfig["USER"]
password := dbConfig["PASSWORD"]
host := dbConfig["HOST"]
portStr := dbConfig["PORT"]
scanner := bufio.NewScanner(file)

if strings.Index(dbEngine, "mysql") >= 0 {
port, err := strconv.ParseInt(portStr, 10, 64)
if err != nil || port <= 0 {
port = 3306
siteRoot := ""
for scanner.Scan() {
line := scanner.Text()
strs := strings.SplitN(line, "=", 2)
if len(strs) != 2 {
continue
}
if dbName == "" {
log.Warnf("Seahub DB name not set in config")
return
key := strs[0]
value := strs[1]
if strings.Index(key, "SECRET_KEY") >= 0 {
value = strings.Trim(value, " ")
seahubPK = strings.Trim(value, "'")
}
if user == "" {
log.Warnf("Seahub DB user not set in config")
return
if strings.Index(key, "SITE_ROOT") >= 0 {
value = strings.Trim(value, " ")
siteRoot = strings.Trim(value, "'")
}
if password == "" {
log.Warnf("Seahub DB password not set in config")
return
}
if host == "" {
log.Warnf("Seahub DB host not set in config")
return
}

dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=%t", user, password, host, port, dbName, false)

seahubDB, err = sql.Open("mysql", dsn)
if err != nil {
log.Warnf("Failed to open database: %v", err)
}
} else if strings.Index(dbEngine, "sqlite") >= 0 {
return
}
if siteRoot != "" {
seahubURL = fmt.Sprintf("http://127.0.0.1:8000%sapi/v2.1/internal/user-list/", siteRoot)
} else {
log.Warnf("Unsupported database %s.", dbEngine)
seahubURL = ("http://127.0.0.1:8000/api/v2.1/internal/user-list/")
}
}

Expand Down Expand Up @@ -433,7 +406,7 @@ func main() {
fp.Close()
}

loadSeahubDB()
loadSeahubPK()

repomgr.Init(seafileDB)

Expand Down
77 changes: 73 additions & 4 deletions fileserver/merge.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"sort"
"strings"
"time"

jwt "github.com/dgrijalva/jwt-go"
"github.com/haiwen/seafile-server/fileserver/commitmgr"
"github.com/haiwen/seafile-server/fileserver/fsmgr"
"github.com/haiwen/seafile-server/fileserver/utils"
)

type mergeOptions struct {
Expand Down Expand Up @@ -379,10 +384,8 @@ func getNickNameByModifier(emailToNickname map[string]string, modifier string) s
if ok {
return nickname
}
if seahubDB != nil {
sqlStr := "SELECT nickname from profile_profile WHERE user = ?"
row := seahubDB.QueryRow(sqlStr, modifier)
row.Scan(&nickname)
if seahubPK != "" {
nickname = postGetNickName(modifier)
}

if nickname == "" {
Expand All @@ -394,6 +397,72 @@ func getNickNameByModifier(emailToNickname map[string]string, modifier string) s
return nickname
}

type SeahubClaims struct {
Exp int64
IsInternal bool `json:"is_internal"`
}

func (*SeahubClaims) Valid() error {
return nil
}

func postGetNickName(modifier string) string {
claims := SeahubClaims{
time.Now().Add(time.Second * 300).Unix(),
true,
}

token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), &claims)
tokenString, err := token.SignedString([]byte(seahubPK))
if err != nil {
return ""
}

header := map[string][]string{
"Authorization": {"Token " + tokenString},
"Content-Type": {"application/json"},
}

data, err := json.Marshal(map[string]interface{}{
"user_id_list": []string{modifier},
})
if err != nil {
return ""
}

status, body, err := utils.HttpCommon("POST", seahubURL, header, bytes.NewReader(data))
if err != nil {
return ""
}
if status != http.StatusOK {
return ""
}

results := make(map[string]interface{})
err = json.Unmarshal(body, &results)
if err != nil {
return ""
}

userList, ok := results["user_list"].([]interface{})
if !ok {
return ""
}
nickname := ""
for _, element := range userList {
list, ok := element.(map[string]interface{})
if !ok {
continue
}
nickname, _ = list["name"].(string)
if nickname != "" {
break
}
}

return nickname
}

func getFileModifierMtime(repoID, storeID, head, filePath string) (string, int64, error) {
commit, err := commitmgr.Load(repoID, head)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions fileserver/utils/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils

import (
"context"
"fmt"
"io"
"net/http"
)

var HttpReqContext, HttpReqCancel = context.WithCancel(context.Background())

func HttpCommon(method, url string, header map[string][]string, reader io.Reader) (int, []byte, error) {
req, err := http.NewRequestWithContext(HttpReqContext, method, url, reader)
if err != nil {
return -1, nil, err
}
req.Header = header

rsp, err := http.DefaultClient.Do(req)
if err != nil {
return -1, nil, err
}
defer rsp.Body.Close()

if rsp.StatusCode == http.StatusNotFound {
return rsp.StatusCode, nil, fmt.Errorf("url %s not found", url)
}
body, err := io.ReadAll(rsp.Body)
if err != nil {
return rsp.StatusCode, nil, err
}

return rsp.StatusCode, body, nil
}
5 changes: 1 addition & 4 deletions server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ http_tx_manager_get_nickname (const char *modifier)
json_t *content = NULL;
json_t *array = NULL;
int rsp_status;
char *req_url = NULL;
char *req_content = NULL;
char *jwt_token = NULL;
char *rsp_content = NULL;
Expand All @@ -509,8 +508,6 @@ http_tx_manager_get_nickname (const char *modifier)
return NULL;
}

req_url = "http://127.0.0.1:8000/api/v2.1/internal/user-list/";

content = json_object ();
array = json_array ();
json_array_append_new (array, json_string (modifier));
Expand All @@ -531,7 +528,7 @@ http_tx_manager_get_nickname (const char *modifier)
g_free (token_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

ret = http_post_common (curl, req_url, jwt_token, req_content, strlen(req_content),
ret = http_post_common (curl, seaf->seahub_url, jwt_token, req_content, strlen(req_content),
&rsp_status, &rsp_content, &rsp_size, TRUE, 1);
if (ret < 0) {
conn->release = TRUE;
Expand Down
1 change: 1 addition & 0 deletions server/seafile-session.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct _SeafileSession {
SeafDB *db;
CcnetDB *ccnet_db;
char *seahub_pk;
char *seahub_url;
ConnectionPool *seahub_conn_pool;

SeafBlockManager *block_mgr;
Expand Down

0 comments on commit 8267b84

Please sign in to comment.