Skip to content

Commit 9d125b4

Browse files
author
epriestley
committedFeb 2, 2016
Use large text columns to store IP addresses
Summary: Fixes T10259. There was no real reason to do this `ip2long()` stuff in the first place -- it's very slightly smaller, but won't work with ipv6 and the savings are miniscule. Test Plan: - Ran migration. - Viewed logs in web UI. - Pulled and pushed. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10259 Differential Revision: https://secure.phabricator.com/D15165
1 parent 1d939e0 commit 9d125b4

10 files changed

+62
-26
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE {$NAMESPACE}_repository.repository_pullevent
2+
CHANGE remoteAddress remoteAddress VARBINARY(64);
3+
4+
ALTER TABLE {$NAMESPACE}_repository.repository_pushevent
5+
CHANGE remoteAddress remoteAddress VARBINARY(64);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
$pull = new PhabricatorRepositoryPullEvent();
4+
$push = new PhabricatorRepositoryPushEvent();
5+
6+
$conn_w = $pull->establishConnection('w');
7+
8+
$log_types = array($pull, $push);
9+
foreach ($log_types as $log) {
10+
foreach (new LiskMigrationIterator($log) as $row) {
11+
$addr = $row->getRemoteAddress();
12+
13+
$addr = (string)$addr;
14+
if (!strlen($addr)) {
15+
continue;
16+
}
17+
18+
if (!ctype_digit($addr)) {
19+
continue;
20+
}
21+
22+
if (!(int)$addr) {
23+
continue;
24+
}
25+
26+
$ip = long2ip($addr);
27+
if (!is_string($ip) || !strlen($ip)) {
28+
continue;
29+
}
30+
31+
$id = $row->getID();
32+
queryfx(
33+
$conn_w,
34+
'UPDATE %T SET remoteAddress = %s WHERE id = %d',
35+
$log->getTableName(),
36+
$ip,
37+
$id);
38+
}
39+
}

‎src/aphront/AphrontRequest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,12 @@ public function isDialogFormPost() {
542542
return $this->isFormPost() && $this->getStr('__dialog__');
543543
}
544544

545-
public function getRemoteAddr() {
546-
return $_SERVER['REMOTE_ADDR'];
545+
public function getRemoteAddress() {
546+
$address = $_SERVER['REMOTE_ADDR'];
547+
if (!strlen($address)) {
548+
return null;
549+
}
550+
return substr($address, 0, 64);
547551
}
548552

549553
public function isHTTPS() {

‎src/applications/config/schema/PhabricatorConfigSchemaSpec.php

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ private function getDetailsForDataType($data_type) {
322322
case 'phid':
323323
case 'policy';
324324
case 'hashpath64':
325+
case 'ipaddress':
325326
$column_type = 'varbinary(64)';
326327
break;
327328
case 'bytes64':

‎src/applications/diffusion/controller/DiffusionServeController.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public function handleRequest(AphrontRequest $request) {
7676
}
7777

7878
try {
79-
$remote_addr = $request->getRemoteAddr();
80-
$remote_addr = ip2long($remote_addr);
79+
$remote_addr = $request->getRemoteAddress();
8180

8281
$pull_event = id(new PhabricatorRepositoryPullEvent())
8382
->setEpoch(PhabricatorTime::getNow())
@@ -720,11 +719,11 @@ private function isValidGitShallowCloneResponse($stdout, $stderr) {
720719
}
721720

722721
private function getCommonEnvironment(PhabricatorUser $viewer) {
723-
$remote_addr = $this->getRequest()->getRemoteAddr();
722+
$remote_address = $this->getRequest()->getRemoteAddress();
724723

725724
return array(
726725
DiffusionCommitHookEngine::ENV_USER => $viewer->getUsername(),
727-
DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS => $remote_addr,
726+
DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS => $remote_address,
728727
DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'http',
729728
);
730729
}

‎src/applications/diffusion/engine/DiffusionCommitHookEngine.php

+1-10
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ public function getRemoteAddress() {
5656
return $this->remoteAddress;
5757
}
5858

59-
private function getRemoteAddressForLog() {
60-
// If whatever we have here isn't a valid IPv4 address, just store `null`.
61-
// Older versions of PHP return `-1` on failure instead of `false`.
62-
$remote_address = $this->getRemoteAddress();
63-
$remote_address = max(0, ip2long($remote_address));
64-
$remote_address = nonempty($remote_address, null);
65-
return $remote_address;
66-
}
67-
6859
public function setSubversionTransactionInfo($transaction, $repository) {
6960
$this->subversionTransaction = $transaction;
7061
$this->subversionRepository = $repository;
@@ -1078,7 +1069,7 @@ private function newPushEvent() {
10781069
$viewer = $this->getViewer();
10791070
return PhabricatorRepositoryPushEvent::initializeNewEvent($viewer)
10801071
->setRepositoryPHID($this->getRepository()->getPHID())
1081-
->setRemoteAddress($this->getRemoteAddressForLog())
1072+
->setRemoteAddress($this->getRemoteAddress())
10821073
->setRemoteProtocol($this->getRemoteProtocol())
10831074
->setEpoch(time());
10841075
}

‎src/applications/diffusion/view/DiffusionPushLogListView.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ public function render() {
4242
$repository = $log->getRepository();
4343

4444
// Reveal this if it's valid and the user can edit the repository.
45-
$remote_addr = '-';
45+
$remote_address = '-';
4646
if (isset($editable_repos[$log->getRepositoryPHID()])) {
47-
$remote_long = $log->getPushEvent()->getRemoteAddress();
48-
if ($remote_long) {
49-
$remote_addr = long2ip($remote_long);
50-
}
47+
$remote_address = $log->getPushEvent()->getRemoteAddress();
5148
}
5249

5350
$event_id = $log->getPushEvent()->getID();
@@ -76,7 +73,7 @@ public function render() {
7673
),
7774
$repository->getDisplayName()),
7875
$handles[$log->getPusherPHID()]->renderLink(),
79-
$remote_addr,
76+
$remote_address,
8077
$log->getPushEvent()->getRemoteProtocol(),
8178
$log->getRefType(),
8279
$log->getRefName(),

‎src/applications/people/view/PhabricatorUserLogView.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function render() {
4141
$ip = phutil_tag(
4242
'a',
4343
array(
44-
'href' => $base_uri.'?ip='.$log->getRemoteAddr().'#R',
44+
'href' => $base_uri.'?ip='.$ip.'#R',
4545
),
4646
$ip);
4747
$session = phutil_tag(
4848
'a',
4949
array(
50-
'href' => $base_uri.'?sessions='.$log->getSession().'#R',
50+
'href' => $base_uri.'?sessions='.$ip.'#R',
5151
),
5252
$session);
5353
}

‎src/applications/repository/storage/PhabricatorRepositoryPullEvent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function getConfiguration() {
3030
self::CONFIG_COLUMN_SCHEMA => array(
3131
'repositoryPHID' => 'phid?',
3232
'pullerPHID' => 'phid?',
33-
'remoteAddress' => 'uint32?',
33+
'remoteAddress' => 'ipaddress?',
3434
'remoteProtocol' => 'text32?',
3535
'resultType' => 'text32',
3636
'resultCode' => 'uint32',

‎src/applications/repository/storage/PhabricatorRepositoryPushEvent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function getConfiguration() {
2929
self::CONFIG_AUX_PHID => true,
3030
self::CONFIG_TIMESTAMPS => false,
3131
self::CONFIG_COLUMN_SCHEMA => array(
32-
'remoteAddress' => 'uint32?',
32+
'remoteAddress' => 'ipaddress?',
3333
'remoteProtocol' => 'text32?',
3434
'rejectCode' => 'uint32',
3535
'rejectDetails' => 'text64?',

0 commit comments

Comments
 (0)
Failed to load comments.