github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/keybase_daemon.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"path/filepath"
    11  
    12  	"github.com/keybase/client/go/kbfs/idutil"
    13  	kbname "github.com/keybase/client/go/kbun"
    14  	"github.com/keybase/client/go/logger"
    15  	"github.com/keybase/client/go/protocol/keybase1"
    16  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    17  )
    18  
    19  // keybaseDaemon is the default KeybaseServiceCn implementation, which
    20  // can use the RPC or local (for debug).
    21  type keybaseDaemon struct{}
    22  
    23  func (k keybaseDaemon) NewKeybaseService(config Config, params InitParams, ctx Context, log logger.Logger) (KeybaseService, error) {
    24  	localUser := kbname.NewNormalizedUsername(params.LocalUser)
    25  	if len(localUser) == 0 {
    26  		err := ctx.ConfigureSocketInfo()
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  
    31  		var additionalProtocols []rpc.Protocol
    32  		for _, creater := range params.AdditionalProtocolCreators {
    33  			p, err := creater(ctx, config)
    34  			if err != nil {
    35  				return nil, err
    36  			}
    37  			additionalProtocols = append(additionalProtocols, p)
    38  		}
    39  
    40  		return NewKeybaseDaemonRPC(
    41  			config, ctx, log, params.Debug, additionalProtocols), nil
    42  	}
    43  
    44  	users := []kbname.NormalizedUsername{
    45  		"strib", "max", "chris", "akalin", "jzila", "alness",
    46  		"jinyang", "songgao", "taru", "zanderz",
    47  	}
    48  	userIndex := -1
    49  	for i := range users {
    50  		if localUser == users[i] {
    51  			userIndex = i
    52  			break
    53  		}
    54  	}
    55  	if userIndex < 0 {
    56  		return nil, fmt.Errorf("user %s not in list %v", localUser, users)
    57  	}
    58  
    59  	localUsers := idutil.MakeLocalUsers(users)
    60  
    61  	// TODO: Auto-generate these, too?
    62  	localUsers[0].Asserts = []string{"github:strib"}
    63  	localUsers[1].Asserts = []string{"twitter:maxtaco"}
    64  	localUsers[2].Asserts = []string{"twitter:malgorithms"}
    65  	localUsers[3].Asserts = []string{"twitter:fakalin"}
    66  	localUsers[4].Asserts = []string{"twitter:jzila"}
    67  	localUsers[5].Asserts = []string{"github:aalness"}
    68  	localUsers[6].Asserts = []string{"github:jinyangli"}
    69  	localUsers[7].Asserts = []string{"github:songgao"}
    70  	// No asserts for 8.
    71  	localUsers[9].Asserts = []string{"github:zanderz"}
    72  
    73  	localUID := localUsers[userIndex].UID
    74  	codec := config.Codec()
    75  
    76  	teams := idutil.MakeLocalTeams(
    77  		[]kbname.NormalizedUsername{"kbfs", "core", "dokan"})
    78  	for i := range teams {
    79  		teams[i].Writers = make(map[keybase1.UID]bool)
    80  		teams[i].Readers = make(map[keybase1.UID]bool)
    81  		switch teams[i].Name {
    82  		case "kbfs":
    83  			teams[i].Writers[localUsers[0].UID] = true // strib
    84  			teams[i].Writers[localUsers[3].UID] = true // akalin
    85  			teams[i].Writers[localUsers[4].UID] = true // jzila
    86  			teams[i].Writers[localUsers[6].UID] = true // jinyang
    87  			teams[i].Writers[localUsers[7].UID] = true // songgao
    88  			teams[i].Writers[localUsers[8].UID] = true // taru
    89  			// readers
    90  			teams[i].Readers[localUsers[1].UID] = true // max
    91  			teams[i].Readers[localUsers[2].UID] = true // chris
    92  		case "core":
    93  			teams[i].Writers[localUsers[1].UID] = true // max
    94  			// readers
    95  			teams[i].Readers[localUsers[0].UID] = true // strib
    96  			teams[i].Readers[localUsers[2].UID] = true // chris
    97  			teams[i].Readers[localUsers[3].UID] = true // akalin
    98  			teams[i].Readers[localUsers[4].UID] = true // jzila
    99  			teams[i].Readers[localUsers[6].UID] = true // jinyang
   100  			teams[i].Readers[localUsers[7].UID] = true // songgao
   101  			teams[i].Readers[localUsers[8].UID] = true // taru
   102  		case "dokan":
   103  			teams[i].Readers[localUsers[8].UID] = true // taru
   104  			teams[i].Readers[localUsers[9].UID] = true // zanderz
   105  		}
   106  	}
   107  
   108  	if params.LocalFavoriteStorage == memoryAddr {
   109  		return NewKeybaseDaemonMemory(localUID, localUsers, teams, codec), nil
   110  	}
   111  
   112  	if serverRootDir, ok := parseRootDir(params.LocalFavoriteStorage); ok {
   113  		favPath := filepath.Join(serverRootDir, "kbfs_favs")
   114  		return NewKeybaseDaemonDisk(localUID, localUsers, teams, favPath, codec)
   115  	}
   116  
   117  	return nil, errors.New("Can't user localuser without LocalFavoriteStorage being 'memory' or 'dir:/path/to/dir'")
   118  }
   119  
   120  func (k keybaseDaemon) NewCrypto(config Config, params InitParams, ctx Context, log logger.Logger) (Crypto, error) {
   121  	var crypto Crypto
   122  	localUser := kbname.NewNormalizedUsername(params.LocalUser)
   123  	if localUser == "" {
   124  		crypto = NewCryptoClientRPC(config, ctx)
   125  	} else {
   126  		signingKey := idutil.MakeLocalUserSigningKeyOrBust(localUser)
   127  		cryptPrivateKey := idutil.MakeLocalUserCryptPrivateKeyOrBust(localUser)
   128  		crypto = NewCryptoLocal(
   129  			config.Codec(), signingKey, cryptPrivateKey, config)
   130  	}
   131  	return crypto, nil
   132  }
   133  
   134  func (k keybaseDaemon) NewChat(
   135  	config Config, params InitParams, ctx Context, log logger.Logger) (
   136  	chat Chat, err error) {
   137  	localUser := kbname.NewNormalizedUsername(params.LocalUser)
   138  	if localUser == "" {
   139  		chat = NewChatRPC(config, ctx)
   140  	} else {
   141  		chat = newChatLocal(config)
   142  	}
   143  	return chat, nil
   144  }