| 1 |
// Package transport includes the implementation for different transport |
| 2 |
// protocols. |
| 3 |
// |
| 4 |
// `Client` can be used to fetch and send packfiles to a git server. |
| 5 |
// The `client` package provides higher level functions to instantiate the |
| 6 |
// appropriate `Client` based on the repository URL. |
| 7 |
// |
| 8 |
// go-git supports HTTP and SSH (see `Protocols`), but you can also install |
| 9 |
// your own protocols (see the `client` package). |
| 10 |
// |
| 11 |
// Each protocol has its own implementation of `Client`, but you should |
| 12 |
// generally not use them directly, use `client.NewClient` instead. |
| 13 |
package transport |
| 14 |
|
| 15 |
import ( |
| 16 |
"bytes" |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"net/url" |
| 22 |
"path/filepath" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
|
| 26 |
giturl "github.com/go-git/go-git/v5/internal/url" |
| 27 |
"github.com/go-git/go-git/v5/plumbing" |
| 28 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 29 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 30 |
) |
| 31 |
|
| 32 |
var ( |
| 33 |
ErrRepositoryNotFound = errors.New("repository not found") |
| 34 |
ErrEmptyRemoteRepository = errors.New("remote repository is empty") |
| 35 |
ErrAuthenticationRequired = errors.New("authentication required") |
| 36 |
ErrAuthorizationFailed = errors.New("authorization failed") |
| 37 |
ErrEmptyUploadPackRequest = errors.New("empty git-upload-pack given") |
| 38 |
ErrInvalidAuthMethod = errors.New("invalid auth method") |
| 39 |
ErrAlreadyConnected = errors.New("session already established") |
| 40 |
) |
| 41 |
|
| 42 |
const ( |
| 43 |
UploadPackServiceName = "git-upload-pack" |
| 44 |
ReceivePackServiceName = "git-receive-pack" |
| 45 |
) |
| 46 |
|
| 47 |
// Transport can initiate git-upload-pack and git-receive-pack processes. |
| 48 |
// It is implemented both by the client and the server, making this a RPC. |
| 49 |
type Transport interface { |
| 50 |
// NewUploadPackSession starts a git-upload-pack session for an endpoint. |
| 51 |
NewUploadPackSession(*Endpoint, AuthMethod) (UploadPackSession, error) |
| 52 |
// NewReceivePackSession starts a git-receive-pack session for an endpoint. |
| 53 |
NewReceivePackSession(*Endpoint, AuthMethod) (ReceivePackSession, error) |
| 54 |
} |
| 55 |
|
| 56 |
type Session interface { |
| 57 |
// AdvertisedReferences retrieves the advertised references for a |
| 58 |
// repository. |
| 59 |
// If the repository does not exist, returns ErrRepositoryNotFound. |
| 60 |
// If the repository exists, but is empty, returns ErrEmptyRemoteRepository. |
| 61 |
AdvertisedReferences() (*packp.AdvRefs, error) |
| 62 |
// AdvertisedReferencesContext retrieves the advertised references for a |
| 63 |
// repository. |
| 64 |
// If the repository does not exist, returns ErrRepositoryNotFound. |
| 65 |
// If the repository exists, but is empty, returns ErrEmptyRemoteRepository. |
| 66 |
AdvertisedReferencesContext(context.Context) (*packp.AdvRefs, error) |
| 67 |
io.Closer |
| 68 |
} |
| 69 |
|
| 70 |
type AuthMethod interface { |
| 71 |
fmt.Stringer |
| 72 |
Name() string |
| 73 |
} |
| 74 |
|
| 75 |
// UploadPackSession represents a git-upload-pack session. |
| 76 |
// A git-upload-pack session has two steps: reference discovery |
| 77 |
// (AdvertisedReferences) and uploading pack (UploadPack). |
| 78 |
type UploadPackSession interface { |
| 79 |
Session |
| 80 |
// UploadPack takes a git-upload-pack request and returns a response, |
| 81 |
// including a packfile. Don't be confused by terminology, the client |
| 82 |
// side of a git-upload-pack is called git-fetch-pack, although here |
| 83 |
// the same interface is used to make it RPC-like. |
| 84 |
UploadPack(context.Context, *packp.UploadPackRequest) (*packp.UploadPackResponse, error) |
| 85 |
} |
| 86 |
|
| 87 |
// ReceivePackSession represents a git-receive-pack session. |
| 88 |
// A git-receive-pack session has two steps: reference discovery |
| 89 |
// (AdvertisedReferences) and receiving pack (ReceivePack). |
| 90 |
// In that order. |
| 91 |
type ReceivePackSession interface { |
| 92 |
Session |
| 93 |
// ReceivePack sends an update references request and a packfile |
| 94 |
// reader and returns a ReportStatus and error. Don't be confused by |
| 95 |
// terminology, the client side of a git-receive-pack is called |
| 96 |
// git-send-pack, although here the same interface is used to make it |
| 97 |
// RPC-like. |
| 98 |
ReceivePack(context.Context, *packp.ReferenceUpdateRequest) (*packp.ReportStatus, error) |
| 99 |
} |
| 100 |
|
| 101 |
// Endpoint represents a Git URL in any supported protocol. |
| 102 |
type Endpoint struct { |
| 103 |
// Protocol is the protocol of the endpoint (e.g. git, https, file). |
| 104 |
Protocol string |
| 105 |
// User is the user. |
| 106 |
User string |
| 107 |
// Password is the password. |
| 108 |
Password string |
| 109 |
// Host is the host. |
| 110 |
Host string |
| 111 |
// Port is the port to connect, if 0 the default port for the given protocol |
| 112 |
// will be used. |
| 113 |
Port int |
| 114 |
// Path is the repository path. |
| 115 |
Path string |
| 116 |
// InsecureSkipTLS skips SSL verification if Protocol is HTTPS. |
| 117 |
InsecureSkipTLS bool |
| 118 |
// ClientCert specifies an optional client certificate to use for mutual |
| 119 |
// TLS authentication if Protocol is HTTPS. |
| 120 |
ClientCert []byte |
| 121 |
// ClientKey specifies an optional client key to use for mutual TLS |
| 122 |
// authentication if Protocol is HTTPS. |
| 123 |
ClientKey []byte |
| 124 |
// CaBundle specifies an optional CA bundle to use for SSL verification |
| 125 |
// if Protocol is HTTPS. The bundle is added in addition to the system |
| 126 |
// CA bundle. |
| 127 |
CaBundle []byte |
| 128 |
// Proxy provides info required for connecting to a proxy. |
| 129 |
Proxy ProxyOptions |
| 130 |
} |
| 131 |
|
| 132 |
type ProxyOptions struct { |
| 133 |
URL string |
| 134 |
Username string |
| 135 |
Password string |
| 136 |
} |
| 137 |
|
| 138 |
func (o *ProxyOptions) Validate() error { |
| 139 |
if o.URL != "" { |
| 140 |
_, err := url.Parse(o.URL) |
| 141 |
return err |
| 142 |
} |
| 143 |
return nil |
| 144 |
} |
| 145 |
|
| 146 |
func (o *ProxyOptions) FullURL() (*url.URL, error) { |
| 147 |
proxyURL, err := url.Parse(o.URL) |
| 148 |
if err != nil { |
| 149 |
return nil, err |
| 150 |
} |
| 151 |
if o.Username != "" { |
| 152 |
if o.Password != "" { |
| 153 |
proxyURL.User = url.UserPassword(o.Username, o.Password) |
| 154 |
} else { |
| 155 |
proxyURL.User = url.User(o.Username) |
| 156 |
} |
| 157 |
} |
| 158 |
return proxyURL, nil |
| 159 |
} |
| 160 |
|
| 161 |
var defaultPorts = map[string]int{ |
| 162 |
"http": 80, |
| 163 |
"https": 443, |
| 164 |
"git": 9418, |
| 165 |
"ssh": 22, |
| 166 |
} |
| 167 |
|
| 168 |
// String returns a string representation of the Git URL. |
| 169 |
func (u *Endpoint) String() string { |
| 170 |
var buf bytes.Buffer |
| 171 |
if u.Protocol != "" { |
| 172 |
buf.WriteString(u.Protocol) |
| 173 |
buf.WriteByte(':') |
| 174 |
} |
| 175 |
|
| 176 |
if u.Protocol != "" || u.Host != "" || u.User != "" || u.Password != "" { |
| 177 |
buf.WriteString("//") |
| 178 |
|
| 179 |
if u.User != "" || u.Password != "" { |
| 180 |
buf.WriteString(url.PathEscape(u.User)) |
| 181 |
if u.Password != "" { |
| 182 |
buf.WriteByte(':') |
| 183 |
buf.WriteString(url.PathEscape(u.Password)) |
| 184 |
} |
| 185 |
|
| 186 |
buf.WriteByte('@') |
| 187 |
} |
| 188 |
|
| 189 |
if u.Host != "" { |
| 190 |
buf.WriteString(u.Host) |
| 191 |
|
| 192 |
if u.Port != 0 { |
| 193 |
port, ok := defaultPorts[strings.ToLower(u.Protocol)] |
| 194 |
if !ok || ok && port != u.Port { |
| 195 |
fmt.Fprintf(&buf, ":%d", u.Port) |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if u.Path != "" && u.Path[0] != '/' && u.Host != "" { |
| 202 |
buf.WriteByte('/') |
| 203 |
} |
| 204 |
|
| 205 |
buf.WriteString(u.Path) |
| 206 |
return buf.String() |
| 207 |
} |
| 208 |
|
| 209 |
func NewEndpoint(endpoint string) (*Endpoint, error) { |
| 210 |
if e, ok := parseSCPLike(endpoint); ok { |
| 211 |
return e, nil |
| 212 |
} |
| 213 |
|
| 214 |
if e, ok := parseFile(endpoint); ok { |
| 215 |
return e, nil |
| 216 |
} |
| 217 |
|
| 218 |
return parseURL(endpoint) |
| 219 |
} |
| 220 |
|
| 221 |
func parseURL(endpoint string) (*Endpoint, error) { |
| 222 |
u, err := url.Parse(endpoint) |
| 223 |
if err != nil { |
| 224 |
return nil, err |
| 225 |
} |
| 226 |
|
| 227 |
if !u.IsAbs() { |
| 228 |
return nil, plumbing.NewPermanentError(fmt.Errorf( |
| 229 |
"invalid endpoint: %s", endpoint, |
| 230 |
)) |
| 231 |
} |
| 232 |
|
| 233 |
var user, pass string |
| 234 |
if u.User != nil { |
| 235 |
user = u.User.Username() |
| 236 |
pass, _ = u.User.Password() |
| 237 |
} |
| 238 |
|
| 239 |
host := u.Hostname() |
| 240 |
if strings.Contains(host, ":") { |
| 241 |
// IPv6 address |
| 242 |
host = "[" + host + "]" |
| 243 |
} |
| 244 |
|
| 245 |
return &Endpoint{ |
| 246 |
Protocol: u.Scheme, |
| 247 |
User: user, |
| 248 |
Password: pass, |
| 249 |
Host: host, |
| 250 |
Port: getPort(u), |
| 251 |
Path: getPath(u), |
| 252 |
}, nil |
| 253 |
} |
| 254 |
|
| 255 |
func getPort(u *url.URL) int { |
| 256 |
p := u.Port() |
| 257 |
if p == "" { |
| 258 |
return 0 |
| 259 |
} |
| 260 |
|
| 261 |
i, err := strconv.Atoi(p) |
| 262 |
if err != nil { |
| 263 |
return 0 |
| 264 |
} |
| 265 |
|
| 266 |
return i |
| 267 |
} |
| 268 |
|
| 269 |
func getPath(u *url.URL) string { |
| 270 |
var res string = u.Path |
| 271 |
if u.RawQuery != "" { |
| 272 |
res += "?" + u.RawQuery |
| 273 |
} |
| 274 |
|
| 275 |
if u.Fragment != "" { |
| 276 |
res += "#" + u.Fragment |
| 277 |
} |
| 278 |
|
| 279 |
return res |
| 280 |
} |
| 281 |
|
| 282 |
func parseSCPLike(endpoint string) (*Endpoint, bool) { |
| 283 |
if giturl.MatchesScheme(endpoint) || !giturl.MatchesScpLike(endpoint) { |
| 284 |
return nil, false |
| 285 |
} |
| 286 |
|
| 287 |
user, host, portStr, path := giturl.FindScpLikeComponents(endpoint) |
| 288 |
port, err := strconv.Atoi(portStr) |
| 289 |
if err != nil { |
| 290 |
port = 22 |
| 291 |
} |
| 292 |
|
| 293 |
return &Endpoint{ |
| 294 |
Protocol: "ssh", |
| 295 |
User: user, |
| 296 |
Host: host, |
| 297 |
Port: port, |
| 298 |
Path: path, |
| 299 |
}, true |
| 300 |
} |
| 301 |
|
| 302 |
func parseFile(endpoint string) (*Endpoint, bool) { |
| 303 |
if giturl.MatchesScheme(endpoint) { |
| 304 |
return nil, false |
| 305 |
} |
| 306 |
|
| 307 |
path, err := filepath.Abs(endpoint) |
| 308 |
if err != nil { |
| 309 |
return nil, false |
| 310 |
} |
| 311 |
|
| 312 |
return &Endpoint{ |
| 313 |
Protocol: "file", |
| 314 |
Path: path, |
| 315 |
}, true |
| 316 |
} |
| 317 |
|
| 318 |
// UnsupportedCapabilities are the capabilities not supported by any client |
| 319 |
// implementation |
| 320 |
var UnsupportedCapabilities = []capability.Capability{ |
| 321 |
capability.MultiACK, |
| 322 |
capability.MultiACKDetailed, |
| 323 |
capability.ThinPack, |
| 324 |
} |
| 325 |
|
| 326 |
// FilterUnsupportedCapabilities it filter out all the UnsupportedCapabilities |
| 327 |
// from a capability.List, the intended usage is on the client implementation |
| 328 |
// to filter the capabilities from an AdvRefs message. |
| 329 |
func FilterUnsupportedCapabilities(list *capability.List) { |
| 330 |
for _, c := range UnsupportedCapabilities { |
| 331 |
list.Delete(c) |
| 332 |
} |
| 333 |
} |