Unverified Commit 838a186a authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Add PKP (and, thus, SOCKS proxy) support

parent 99cb6a7e
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -35,6 +35,17 @@
		password => die("Changeme!"),
	},

	# Settings specific to HAFAS backends.
	# For instance, the PKP backend is hidden behind a GeoIP filter, hence
	# travelynx only supports it if travelynx.conf either indicates that it
	# is reachable or specifies a proxy.
	hafas => {
		PKP => {
			# geoip_ok => 1,          # <- EITHER THIS
			# proxy => 'socks://...', # <- OR THIS
		},
	},

	# These settings control the amount and (re)spawn behaviour of travelynx
	# worker processes as well as IP, port, and PID file. They are suitable for
	# up to a few dozen concurrent users. If your site has more traffic, you
+1 −0
Original line number Diff line number Diff line
@@ -221,6 +221,7 @@ sub startup {
			my ($self) = @_;
			state $hafas = Travelynx::Helper::HAFAS->new(
				log            => $self->app->log,
				service_config => $self->app->config->{hafas},
				main_cache     => $self->app->cache_iris_main,
				realtime_cache => $self->app->cache_iris_rt,
				root_url       => $self->base_url_for('/')->to_abs,
+11 −0
Original line number Diff line number Diff line
@@ -1079,6 +1079,17 @@ sub backend_form {
			{
				$type = undef;
			}

			# PKP is behind a GeoIP filter. Only list it if travelynx.conf
			# indicates that our IP is allowed or provides a proxy.
			elsif (
				$backend->{name} eq 'PKP'
				and not( $self->app->config->{hafas}{PKP}{geoip_ok}
					or $self->app->config->{hafas}{PKP}{proxy} )
			  )
			{
				$type = undef;
			}
			elsif ( my $s = $self->hafas->get_service( $backend->{name} ) ) {
				$type                = 'HAFAS';
				$backend->{longname} = $s->{name};
+9 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use DateTime::Format::Strptime;
use List::Util      qw(uniq min max);
use List::UtilsBy   qw(max_by uniq_by);
use List::MoreUtils qw(first_index);
use Mojo::UserAgent;
use Mojo::Promise;
use Text::CSV;
use Travel::Status::DE::IRIS::Stations;
@@ -529,9 +530,16 @@ sub geolocation {
	if ($hafas_service) {
		$self->render_later;

		my $agent = $self->ua;
		if ( my $proxy = $self->app->config->{hafas}{$hafas_service}{proxy} ) {
			$agent = Mojo::UserAgent->new;
			$agent->proxy->http($proxy);
			$agent->proxy->https($proxy);
		}

		Travel::Status::DE::HAFAS->new_p(
			promise    => 'Mojo::Promise',
			user_agent => $self->ua,
			user_agent => $agent,
			service    => $hafas_service,
			geoSearch  => {
				lat => $lat,
+56 −10
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ use DateTime;
use Encode qw(decode);
use JSON;
use Mojo::Promise;
use Mojo::UserAgent;
use Travel::Status::DE::HAFAS;

sub _epoch {
@@ -42,32 +43,50 @@ sub get_service {
sub get_departures_p {
	my ( $self, %opt ) = @_;

	$opt{service} //= 'VRN';

	my $agent = $self->{user_agent};
	if ( my $proxy = $self->{service_config}{ $opt{service} }{proxy} ) {
		$agent = Mojo::UserAgent->new;
		$agent->proxy->http($proxy);
		$agent->proxy->https($proxy);
	}

	my $when = (
		  $opt{timestamp}
		? $opt{timestamp}->clone
		: DateTime->now( time_zone => 'Europe/Berlin' )
	)->subtract( minutes => $opt{lookbehind} );
	return Travel::Status::DE::HAFAS->new_p(
		service    => $opt{service} // 'VRN',
		service    => $opt{service},
		station    => $opt{eva},
		datetime   => $when,
		lookahead  => $opt{lookahead} + $opt{lookbehind},
		results    => 300,
		cache      => $self->{realtime_cache},
		promise    => 'Mojo::Promise',
		user_agent => $self->{user_agent}->request_timeout(5),
		user_agent => $agent->request_timeout(5),
	);
}

sub search_location_p {
	my ( $self, %opt ) = @_;

	$opt{service} //= 'VRN';

	my $agent = $self->{user_agent};
	if ( my $proxy = $self->{service_config}{ $opt{service} }{proxy} ) {
		$agent = Mojo::UserAgent->new;
		$agent->proxy->http($proxy);
		$agent->proxy->https($proxy);
	}

	return Travel::Status::DE::HAFAS->new_p(
		service        => $opt{service} // 'VRN',
		service        => $opt{service},
		locationSearch => $opt{query},
		cache          => $self->{realtime_cache},
		promise        => 'Mojo::Promise',
		user_agent     => $self->{user_agent}->request_timeout(5),
		user_agent     => $agent->request_timeout(5),
	);
}

@@ -80,13 +99,22 @@ sub get_tripid_p {
	my $train_desc = $train->type . ' ' . $train->train_no;
	$train_desc =~ s{^- }{};

	$opt{service} //= 'VRN';

	my $agent = $self->{user_agent};
	if ( my $proxy = $self->{service_config}{ $opt{service} }{proxy} ) {
		$agent = Mojo::UserAgent->new;
		$agent->proxy->http($proxy);
		$agent->proxy->https($proxy);
	}

	Travel::Status::DE::HAFAS->new_p(
		service      => $opt{service} // 'VRN',
		service      => $opt{service},
		journeyMatch => $train_desc,
		datetime     => $train->start,
		cache        => $self->{realtime_cache},
		promise      => 'Mojo::Promise',
		user_agent   => $self->{user_agent}->request_timeout(10),
		user_agent   => $agent->request_timeout(10),
	)->then(
		sub {
			my ($hafas) = @_;
@@ -132,15 +160,24 @@ sub get_journey_p {
	my $promise = Mojo::Promise->new;
	my $now     = DateTime->now( time_zone => 'Europe/Berlin' );

	$opt{service} //= 'VRN';

	my $agent = $self->{user_agent};
	if ( my $proxy = $self->{service_config}{ $opt{service} }{proxy} ) {
		$agent = Mojo::UserAgent->new;
		$agent->proxy->http($proxy);
		$agent->proxy->https($proxy);
	}

	Travel::Status::DE::HAFAS->new_p(
		service => $opt{service} // 'VRN',
		service => $opt{service},
		journey => {
			id => $opt{trip_id},
		},
		with_polyline => $opt{with_polyline},
		cache         => $self->{realtime_cache},
		promise       => 'Mojo::Promise',
		user_agent    => $self->{user_agent}->request_timeout(10),
		user_agent    => $agent->request_timeout(10),
	)->then(
		sub {
			my ($hafas) = @_;
@@ -173,8 +210,17 @@ sub get_route_p {
	my $promise = Mojo::Promise->new;
	my $now     = DateTime->now( time_zone => 'Europe/Berlin' );

	$opt{service} //= 'VRN';

	my $agent = $self->{user_agent};
	if ( my $proxy = $self->{service_config}{ $opt{service} }{proxy} ) {
		$agent = Mojo::UserAgent->new;
		$agent->proxy->http($proxy);
		$agent->proxy->https($proxy);
	}

	Travel::Status::DE::HAFAS->new_p(
		service => $opt{service} // 'VRN',
		service => $opt{service},
		journey => {
			id => $opt{trip_id},

@@ -183,7 +229,7 @@ sub get_route_p {
		with_polyline => $opt{with_polyline},
		cache         => $self->{realtime_cache},
		promise       => 'Mojo::Promise',
		user_agent    => $self->{user_agent}->request_timeout(10),
		user_agent    => $agent->request_timeout(10),
	)->then(
		sub {
			my ($hafas) = @_;