Skip to content

Commit

Permalink
Added PSR-15 support
Browse files Browse the repository at this point in the history
  • Loading branch information
noglitchyo committed Jun 20, 2019
1 parent d50a03a commit e5cbc7a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It can be use as a middleware or a client and attempt to provide a low-level abs
## Description

Dealdoh can be use in different manners and for different purposes. Dealdoh attempt to achieve the following goals:
- provide a DoH middleware which can be use in any PHP application acting as a DNS proxy.
- provide a DoH middleware PSR-15 compliant which can be use in any PHP application to act as a DNS proxy.
- provide a variety of DNS stub resolver.
- provide a large panel of DNS clients.
- provide a low-level abstraction layer for development around DNS.
Expand Down Expand Up @@ -55,15 +55,15 @@ To get trusted certificates in a local environment, I recommend you to use [mkce

`composer require noglitchyo/dealdoh`

- You will need a PSR-7 ServerRequest if you wish to directly use the `HttpProxy::forward()` method.
- You will need a PSR-7 ServerRequest if you wish to directly use the `DohProxy::forward()` method.
Please check those cool implementations below:
* https://github.com/Nyholm/psr7 - `composer require nyholm/psr7`
* https://github.com/guzzle/psr7 - `composer require guzzle/psr7`
* https://github.com/zendframework/zend-diactoros - `composer require zendframework/zend-diactoros`

- Configure your middleware/entrypoint to call Dealdoh's `HttpProxy::forward()`
- Configure your middleware/entrypoint to call Dealdoh's `DohProxy::forward()`

As stated before, `HttpProxy::forward()` method consumes PSR-7 ServerRequest to make the integration easier.
As stated before, `DohProxy::forward()` method consumes PSR-7 ServerRequest to make the integration easier.

The example below illustrates how to use two DNS upstream resolvers which are using different protocols.
In this example, the used protocols are TCP/UDP (RFC-1035) and DoH (RFC-8484).
Expand All @@ -89,7 +89,7 @@ $dnsResolver = new \NoGlitchYo\Dealdoh\Service\DnsPoolResolver(
]
);

$dnsProxy = new \NoGlitchYo\Dealdoh\HttpProxy(
$dnsProxy = new \NoGlitchYo\Dealdoh\DohProxy(
$dnsResolver,
$dnsMessageFactory,
new \NoGlitchYo\Dealdoh\Factory\DohHttpMessageFactory($dnsMessageFactory)
Expand Down Expand Up @@ -177,5 +177,6 @@ Combined with Dealdoh it is amazing.
- [RFC-4501](https://tools.ietf.org/html/rfc4501)
- [RFC-7719](https://tools.ietf.org/html/rfc7719)
- [PSR-7](https://www.php-fig.org/psr/psr-7/)
- [PSR-15](https://www.php-fig.org/psr/psr-15/)
- [PSR-18](https://www.php-fig.org/psr/psr-18/)
- [Wiki page DNS-over-HTTPS from Curl](https://github.com/curl/curl/wiki/DNS-over-HTTPS)
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"php": "^7.3",
"psr/http-client": "*",
"psr/log": "^1.1",
"psr/http-server-middleware": "^1.0",
"php-http/guzzle6-adapter": "^2.0",
"react/dns": "^0.4.17"
},
Expand Down
6 changes: 3 additions & 3 deletions examples/docker-firefox/docker/src/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use Http\Adapter\Guzzle6\Client;
use NoGlitchYo\Dealdoh\Client\DohClient;
use NoGlitchYo\Dealdoh\Client\StdClient;
use NoGlitchYo\Dealdoh\Service\DnsPoolResolver;
use NoGlitchYo\Dealdoh\DohProxy;
use NoGlitchYo\Dealdoh\Entity\DnsUpstreamPool;
use NoGlitchYo\Dealdoh\Factory\Dns\MessageFactory;
use NoGlitchYo\Dealdoh\Factory\DohHttpMessageFactory;
use NoGlitchYo\Dealdoh\HttpProxy;
use NoGlitchYo\Dealdoh\Service\DnsPoolResolver;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App;
Expand Down Expand Up @@ -43,7 +43,7 @@ function (ServerRequestInterface $request, ResponseInterface $response, $args) {
]
);

$dnsProxy = new HttpProxy(
$dnsProxy = new DohProxy(
$dnsResolver,
$dnsMessageFactory,
new DohHttpMessageFactory($dnsMessageFactory)
Expand Down
6 changes: 3 additions & 3 deletions examples/slim-integration/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use Http\Adapter\Guzzle6\Client;
use NoGlitchYo\Dealdoh\Client\DohClient;
use NoGlitchYo\Dealdoh\Client\StdClient;
use NoGlitchYo\Dealdoh\Service\DnsPoolResolver;
use NoGlitchYo\Dealdoh\DohProxy;
use NoGlitchYo\Dealdoh\Entity\DnsUpstreamPool;
use NoGlitchYo\Dealdoh\Factory\Dns\MessageFactory;
use NoGlitchYo\Dealdoh\Factory\DohHttpMessageFactory;
use NoGlitchYo\Dealdoh\HttpProxy;
use NoGlitchYo\Dealdoh\Service\DnsPoolResolver;
use Slim\App;
use Socket\Raw\Factory;

Expand Down Expand Up @@ -40,7 +40,7 @@ function (ServerRequestInterface $request, ResponseInterface $response, $args) {
]
);

$dnsProxy = new HttpProxy(
$dnsProxy = new DohProxy(
$dnsResolver,
$dnsMessageFactory,
new DohHttpMessageFactory($dnsMessageFactory)
Expand Down
10 changes: 9 additions & 1 deletion src/HttpProxy.php → src/DohProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;

class HttpProxy
class DohProxy implements MiddlewareInterface
{
/**
* @var DnsResolverInterface
Expand Down Expand Up @@ -48,6 +50,12 @@ public function __construct(
$this->dohHttpMessageFactory = $dohHttpMessageFactory;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->forward($request);
}


/**
* @throws HttpProxyException
*/
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/HttpProxyTest.php → tests/Unit/DohProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\MockInterface;
use NoGlitchYo\Dealdoh\Client\DnsClientInterface;
use NoGlitchYo\Dealdoh\DohProxy;
use NoGlitchYo\Dealdoh\Entity\Dns\Message;
use NoGlitchYo\Dealdoh\Entity\Dns\MessageInterface;
use NoGlitchYo\Dealdoh\Entity\DnsResource;
Expand All @@ -15,7 +16,6 @@
use NoGlitchYo\Dealdoh\Factory\Dns\MessageFactoryInterface;
use NoGlitchYo\Dealdoh\Factory\DohHttpMessageFactoryInterface;
use NoGlitchYo\Dealdoh\Helper\Base64UrlCodecHelper;
use NoGlitchYo\Dealdoh\HttpProxy;
use NoGlitchYo\Dealdoh\Service\DnsResolverInterface;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
Expand All @@ -24,9 +24,9 @@
use Psr\Log\LoggerInterface;

/**
* @covers \NoGlitchYo\Dealdoh\HttpProxy
* @covers \NoGlitchYo\Dealdoh\DohProxy
*/
class HttpProxyTest extends TestCase
class DohProxyTest extends TestCase
{
use MockeryPHPUnitIntegration;

Expand All @@ -36,7 +36,7 @@ class HttpProxyTest extends TestCase
private $dnsResolverMock;

/**
* @var MockInterface|\NoGlitchYo\Dealdoh\Factory\Dns\MessageFactoryInterface
* @var MockInterface|MessageFactoryInterface
*/
private $dnsMessageFactoryMock;

Expand All @@ -46,7 +46,7 @@ class HttpProxyTest extends TestCase
private $loggerMock;

/**
* @var HttpProxy
* @var DohProxy
*/
private $sut;

Expand All @@ -62,7 +62,7 @@ protected function setUp(): void
$this->dohHttpMessageFactoryMock = Mockery::mock(DohHttpMessageFactoryInterface::class);
$this->loggerMock = Mockery::mock(LoggerInterface::class);

$this->sut = new HttpProxy(
$this->sut = new DohProxy(
$this->dnsResolverMock,
$this->dnsMessageFactoryMock,
$this->dohHttpMessageFactoryMock,
Expand Down

0 comments on commit e5cbc7a

Please sign in to comment.