-
Notifications
You must be signed in to change notification settings - Fork 276
Add proxy functionality #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
825956b
Add proxy to options
mosuem 8a3b47c
Add proxy connect
mosuem 7cf4677
Works now
mosuem 4f74e51
Uncomment proxy line
mosuem ae0f6ae
Revert change
mosuem 1f7dfdc
Doesn't work
mosuem db47af7
Works
mosuem 309e2db
Fix bug
mosuem 4e36f42
Add secure test
mosuem 3ae7a05
Merge branch 'master' into addProxy
mosuem 7195e35
Refine test
mosuem 1e5343c
Add changelog
mosuem 2f1be18
Changes as per review
mosuem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
class Proxy { | ||
final String host; | ||
final int port; | ||
final String? username; | ||
final String? password; | ||
|
||
const Proxy({ | ||
required this.host, | ||
required this.port, | ||
this.username, | ||
this.password, | ||
}); | ||
|
||
bool get isAuthenticated => username != null; | ||
} |
This file contains hidden or 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,75 @@ | ||
@TestOn('vm') | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:grpc/grpc.dart'; | ||
import 'package:grpc/src/client/proxy.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'src/generated/echo.pbgrpc.dart'; | ||
|
||
void main() { | ||
late Server server; | ||
late EchoServiceClient fakeClient; | ||
late ClientChannel fakeChannel; | ||
|
||
setUp(() async { | ||
server = Server.create(services: [FakeEchoService()]); | ||
await server.serve( | ||
address: 'localhost', | ||
port: 8888, | ||
security: ServerTlsCredentials( | ||
certificate: File('test/data/localhost.crt').readAsBytesSync(), | ||
privateKey: File('test/data/localhost.key').readAsBytesSync(), | ||
), | ||
); | ||
final proxy = Proxy(host: 'localhost', port: 8080); | ||
final proxyCAName = '/CN=mitmproxy/O=mitmproxy'; | ||
|
||
fakeChannel = ClientChannel( | ||
'localhost', | ||
port: server.port!, | ||
options: ChannelOptions( | ||
credentials: ChannelCredentials.secure( | ||
certificates: File('test/data/localhost.crt').readAsBytesSync(), | ||
authority: 'localhost', | ||
onBadCertificate: (certificate, host) { | ||
/// Workaround to avoid having to add the proxy to our list of | ||
/// trusted CAs. | ||
return certificate.issuer == proxyCAName; | ||
}, | ||
), | ||
proxy: proxy, | ||
), | ||
); | ||
fakeClient = EchoServiceClient(fakeChannel); | ||
}); | ||
|
||
tearDown(() async { | ||
await fakeChannel.shutdown(); | ||
await server.shutdown(); | ||
}); | ||
|
||
test( | ||
'Sending and receiving over secure proxy works', | ||
() async { | ||
final echoRequest = EchoRequest(message: 'blablablubb'); | ||
final echoResponse = await fakeClient.echo(echoRequest); | ||
expect(echoResponse.message, 'blibliblabb'); | ||
}, | ||
skip: 'Run this test iff you have a proxy running.', | ||
); | ||
} | ||
|
||
class FakeEchoService extends EchoServiceBase { | ||
@override | ||
Future<EchoResponse> echo(ServiceCall call, EchoRequest request) async { | ||
expect(request.message, 'blablablubb'); | ||
return EchoResponse(message: 'blibliblabb'); | ||
} | ||
|
||
@override | ||
Stream<ServerStreamingEchoResponse> serverStreamingEcho( | ||
ServiceCall call, ServerStreamingEchoRequest request) => | ||
throw UnimplementedError(); | ||
} |
This file contains hidden or 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,57 @@ | ||
@TestOn('vm') | ||
import 'dart:async'; | ||
|
||
import 'package:grpc/grpc.dart'; | ||
import 'package:grpc/src/client/proxy.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'src/generated/echo.pbgrpc.dart'; | ||
|
||
void main() { | ||
late Server server; | ||
late EchoServiceClient fakeClient; | ||
late ClientChannel fakeChannel; | ||
|
||
setUp(() async { | ||
server = Server.create(services: [FakeEchoService()]); | ||
await server.serve(address: 'localhost', port: 8888); | ||
|
||
final proxy = Proxy(host: 'localhost', port: 8080); | ||
|
||
fakeChannel = ClientChannel( | ||
'localhost', | ||
port: server.port!, | ||
options: ChannelOptions( | ||
credentials: ChannelCredentials.insecure(), | ||
proxy: proxy, | ||
), | ||
); | ||
fakeClient = EchoServiceClient(fakeChannel); | ||
}); | ||
|
||
tearDown(() async { | ||
await fakeChannel.shutdown(); | ||
await server.shutdown(); | ||
}); | ||
|
||
test( | ||
'Sending and receiving over proxy works', | ||
() async { | ||
final echoRequest = EchoRequest(message: 'blablablubb'); | ||
final echoResponse = await fakeClient.echo(echoRequest); | ||
expect(echoResponse.message, 'blibliblabb'); | ||
}, | ||
skip: 'Run this test iff you have a proxy running.', | ||
); | ||
} | ||
|
||
class FakeEchoService extends EchoServiceBase { | ||
@override | ||
Future<EchoResponse> echo(ServiceCall call, EchoRequest request) async => | ||
EchoResponse(message: 'blibliblabb'); | ||
|
||
@override | ||
Stream<ServerStreamingEchoResponse> serverStreamingEcho( | ||
ServiceCall call, ServerStreamingEchoRequest request) => | ||
throw UnimplementedError(); | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.