Skip to content

Commit 1dc4491

Browse files
author
Edward Thomson
committed
Merge pull request libgit2#3110 from libgit2/cmn/proxy-config
Proxy configuration
2 parents 95fbc81 + 2638df7 commit 1dc4491

File tree

30 files changed

+574
-71
lines changed

30 files changed

+574
-71
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ IF (BUILD_CLAR)
690690
# Add a test target which runs the cred callback tests, to be
691691
# called after setting the url and user
692692
ADD_TEST(libgit2_clar-cred_callback libgit2_clar -v -sonline::clone::cred_callback)
693+
ADD_TEST(libgit2_clar-proxy_credentials_in_url libgit2_clar -v -sonline::clone::proxy_credentials_in_url)
694+
ADD_TEST(libgit2_clar-proxy_credentials_request libgit2_clar -v -sonline::clone::proxy_credentials_request)
693695
ENDIF ()
694696

695697
IF (TAGS)

appveyor.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ environment:
1919
cache:
2020
- i686-4.9.2-release-win32-sjlj-rt_v3-rev1.7z
2121
- x86_64-4.9.2-release-win32-seh-rt_v3-rev1.7z
22+
2223
build_script:
2324
- ps: |
2425
mkdir build
@@ -32,7 +33,17 @@ build_script:
3233
test_script:
3334
- ps: |
3435
$ErrorActionPreference="Stop"
36+
Invoke-WebRequest https://github.com/ethomson/poxyproxy/releases/download/v0.1.0/poxyproxy-0.1.0.jar -OutFile poxyproxy.jar
37+
# Run this early so we know it's ready by the time we need it
38+
$proxyJob = Start-Job { java -jar $Env:APPVEYOR_BUILD_FOLDER\build\poxyproxy.jar -d --port 8080 --credentials foo:bar }
3539
ctest -V -R libgit2_clar
3640
$env:GITTEST_REMOTE_URL="https://github.com/libgit2/non-existent"
3741
$env:GITTEST_REMOTE_USER="libgit2test"
3842
ctest -V -R libgit2_clar-cred_callback
43+
Receive-Job -Job $proxyJob
44+
$env:GITTEST_REMOTE_PROXY_URL = "http://foo:bar@localhost:8080"
45+
ctest -V -R libgit2_clar-proxy_credentials_in_url
46+
$env:GITTEST_REMOTE_PROXY_URL = "http://localhost:8080"
47+
$env:GITTEST_REMOTE_PROXY_USER = "foo"
48+
$env:GITTEST_REMOTE_PROXY_PASS = "bar"
49+
ctest -V -R libgit2_clar-proxy_credentials_request

examples/network/ls-remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static int use_remote(git_repository *repo, char *name)
2626
*/
2727
callbacks.credentials = cred_acquire_cb;
2828

29-
error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL);
29+
error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL);
3030
if (error < 0)
3131
goto cleanup;
3232

include/git2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "git2/pack.h"
4141
#include "git2/patch.h"
4242
#include "git2/pathspec.h"
43+
#include "git2/proxy.h"
4344
#include "git2/rebase.h"
4445
#include "git2/refdb.h"
4546
#include "git2/reflog.h"

include/git2/proxy.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_git_proxy_h__
8+
#define INCLUDE_git_proxy_h__
9+
10+
#include "common.h"
11+
#include "transport.h"
12+
13+
GIT_BEGIN_DECL
14+
15+
/**
16+
* The type of proxy to use.
17+
*/
18+
typedef enum {
19+
/**
20+
* Do not attempt to connect through a proxy
21+
*
22+
* If built against lbicurl, it itself may attempt to connect
23+
* to a proxy if the environment variables specify it.
24+
*/
25+
GIT_PROXY_NONE,
26+
/**
27+
* Try to auto-detect the proxy from the git configuration.
28+
*/
29+
GIT_PROXY_AUTO,
30+
/**
31+
* Connect via the URL given in the options
32+
*/
33+
GIT_PROXY_SPECIFIED,
34+
} git_proxy_t;
35+
36+
/**
37+
* Options for connecting through a proxy
38+
*
39+
* Note that not all types may be supported, depending on the platform
40+
* and compilation options.
41+
*/
42+
typedef struct {
43+
unsigned int version;
44+
45+
/**
46+
* The type of proxy to use, by URL, auto-detect.
47+
*/
48+
git_proxy_t type;
49+
50+
/**
51+
* The URL of the proxy.
52+
*/
53+
const char *url;
54+
55+
/**
56+
* This will be called if the remote host requires
57+
* authentication in order to connect to it.
58+
*
59+
* Returning GIT_PASSTHROUGH will make libgit2 behave as
60+
* though this field isn't set.
61+
*/
62+
git_cred_acquire_cb credentials;
63+
64+
/**
65+
* If cert verification fails, this will be called to let the
66+
* user make the final decision of whether to allow the
67+
* connection to proceed. Returns 1 to allow the connection, 0
68+
* to disallow it or a negative value to indicate an error.
69+
*/
70+
git_transport_certificate_check_cb certificate_check;
71+
72+
/**
73+
* Payload to be provided to the credentials and certificate
74+
* check callbacks.
75+
*/
76+
void *payload;
77+
} git_proxy_options;
78+
79+
#define GIT_PROXY_OPTIONS_VERSION 1
80+
#define GIT_PROXY_OPTIONS_INIT {GIT_PROXY_OPTIONS_VERSION}
81+
82+
/**
83+
* Initialize a proxy options structure
84+
*
85+
* @param opts the options struct to initialize
86+
* @param version the version of the struct, use `GIT_PROXY_OPTIONS_VERSION`
87+
*/
88+
GIT_EXTERN(int) git_proxy_init_options(git_proxy_options *opts, unsigned int version);
89+
90+
GIT_END_DECL
91+
92+
#endif

include/git2/remote.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "strarray.h"
1616
#include "transport.h"
1717
#include "pack.h"
18+
#include "proxy.h"
1819

1920
/**
2021
* @file git2/remote.h
@@ -241,10 +242,11 @@ GIT_EXTERN(const git_refspec *)git_remote_get_refspec(const git_remote *remote,
241242
* @param direction GIT_DIRECTION_FETCH if you want to fetch or
242243
* GIT_DIRECTION_PUSH if you want to push
243244
* @param callbacks the callbacks to use for this connection
245+
* @param proxy_opts proxy settings
244246
* @param custom_headers extra HTTP headers to use in this connection
245247
* @return 0 or an error code
246248
*/
247-
GIT_EXTERN(int) git_remote_connect(git_remote *remote, git_direction direction, const git_remote_callbacks *callbacks, const git_strarray *custom_headers);
249+
GIT_EXTERN(int) git_remote_connect(git_remote *remote, git_direction direction, const git_remote_callbacks *callbacks, const git_proxy_options *proxy_opts, const git_strarray *custom_headers);
248250

249251
/**
250252
* Get the remote repository's reference advertisement list
@@ -548,14 +550,20 @@ typedef struct {
548550
*/
549551
git_remote_autotag_option_t download_tags;
550552

553+
/**
554+
* Proxy options to use, by default no proxy is used.
555+
*/
556+
git_proxy_options proxy_opts;
557+
551558
/**
552559
* Extra headers for this fetch operation
553560
*/
554561
git_strarray custom_headers;
555562
} git_fetch_options;
556563

557564
#define GIT_FETCH_OPTIONS_VERSION 1
558-
#define GIT_FETCH_OPTIONS_INIT { GIT_FETCH_OPTIONS_VERSION, GIT_REMOTE_CALLBACKS_INIT, GIT_FETCH_PRUNE_UNSPECIFIED, 1 }
565+
#define GIT_FETCH_OPTIONS_INIT { GIT_FETCH_OPTIONS_VERSION, GIT_REMOTE_CALLBACKS_INIT, GIT_FETCH_PRUNE_UNSPECIFIED, 1, \
566+
GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT }
559567

560568
/**
561569
* Initializes a `git_fetch_options` with default values. Equivalent to
@@ -592,14 +600,19 @@ typedef struct {
592600
*/
593601
git_remote_callbacks callbacks;
594602

603+
/**
604+
* Proxy options to use, by default no proxy is used.
605+
*/
606+
git_proxy_options proxy_opts;
607+
595608
/**
596609
* Extra headers for this push operation
597610
*/
598611
git_strarray custom_headers;
599612
} git_push_options;
600613

601614
#define GIT_PUSH_OPTIONS_VERSION 1
602-
#define GIT_PUSH_OPTIONS_INIT { GIT_PUSH_OPTIONS_VERSION, 0, GIT_REMOTE_CALLBACKS_INIT }
615+
#define GIT_PUSH_OPTIONS_INIT { GIT_PUSH_OPTIONS_VERSION, 0, GIT_REMOTE_CALLBACKS_INIT, GIT_PROXY_OPTIONS_INIT }
603616

604617
/**
605618
* Initializes a `git_push_options` with default values. Equivalent to

include/git2/sys/remote.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_sys_git_transport_h
9+
#define INCLUDE_sys_git_transport_h
10+
11+
#include "git2/net.h"
12+
#include "git2/types.h"
13+
14+
GIT_BEGIN_DECL
15+
16+
GIT_END_DECL

include/git2/sys/stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "git2/common.h"
1111
#include "git2/types.h"
12+
#include "git2/proxy.h"
1213

1314
GIT_BEGIN_DECL
1415

@@ -32,7 +33,7 @@ typedef struct git_stream {
3233
int proxy_support;
3334
int (*connect)(struct git_stream *);
3435
int (*certificate)(git_cert **, struct git_stream *);
35-
int (*set_proxy)(struct git_stream *, const char *proxy_url);
36+
int (*set_proxy)(struct git_stream *, const git_proxy_options *proxy_opts);
3637
ssize_t (*read)(struct git_stream *, void *, size_t);
3738
ssize_t (*write)(struct git_stream *, const char *, size_t, int);
3839
int (*close)(struct git_stream *);

include/git2/sys/transport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "git2/net.h"
1212
#include "git2/types.h"
1313
#include "git2/strarray.h"
14+
#include "git2/proxy.h"
1415

1516
/**
1617
* @file git2/sys/transport.h
@@ -53,6 +54,7 @@ struct git_transport {
5354
const char *url,
5455
git_cred_acquire_cb cred_acquire_cb,
5556
void *cred_acquire_payload,
57+
const git_proxy_options *proxy_opts,
5658
int direction,
5759
int flags);
5860

@@ -65,7 +67,7 @@ struct git_transport {
6567
git_transport *transport);
6668

6769
/* Executes the push whose context is in the git_push object. */
68-
int (*push)(git_transport *transport, git_push *push, const git_remote_callbacks *callbacks);
70+
int(*push)(git_transport *transport, git_push *push, const git_remote_callbacks *callbacks);
6971

7072
/* This function may be called after a successful call to connect(), when
7173
* the direction is FETCH. The function performs a negotiation to calculate

script/cibuild.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ then
66
exit $?;
77
fi
88

9+
# Should we ask Travis to cache this file?
10+
curl -L https://github.com/ethomson/poxyproxy/releases/download/v0.1.0/poxyproxy-0.1.0.jar >poxyproxy.jar || exit $?
11+
# Run this early so we know it's ready by the time we need it
12+
java -jar poxyproxy.jar -d --port 8080 --credentials foo:bar &
13+
914
mkdir _build
1015
cd _build
1116
# shellcheck disable=SC2086
@@ -49,12 +54,22 @@ export GITTEST_REMOTE_SSH_KEY="$HOME/.ssh/id_rsa"
4954
export GITTEST_REMOTE_SSH_PUBKEY="$HOME/.ssh/id_rsa.pub"
5055
export GITTEST_REMOTE_SSH_PASSPHRASE=""
5156

57+
5258
if [ -e ./libgit2_clar ]; then
5359
./libgit2_clar -sonline::push -sonline::clone::ssh_cert &&
5460
./libgit2_clar -sonline::clone::ssh_with_paths || exit $?
5561
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
5662
./libgit2_clar -sonline::clone::cred_callback || exit $?
5763
fi
64+
65+
# Use the proxy we started at the beginning
66+
export GITTEST_REMOTE_PROXY_URL="http://foo:bar@localhost:8080/"
67+
./libgit2_clar -sonline::clone::proxy_credentials_in_url || exit $?
68+
export GITTEST_REMOTE_PROXY_URL="http://localhost:8080/"
69+
export GITTEST_REMOTE_PROXY_USER="foo"
70+
export GITTEST_REMOTE_PROXY_PASS="bar"
71+
./libgit2_clar -sonline::clone::proxy_credentials_request || exit $?
72+
5873
fi
5974

6075
export GITTEST_REMOTE_URL="https://github.com/libgit2/non-existent"

0 commit comments

Comments
 (0)