2

I have quite happily set up pac files using myIpAddress() as a function to resolve the local IP on my LAN to load balance my proxies.

I now need to use this function, or anything that works simply, to return the local ip so that I can change the content of my media server to deliver hq video to high bandwidth pcs while delivering lower quality to the area offices which are on a different subnet. Searching has proved futile.

The outcome is to have something that allows a web page to display http://mediaserver/x to one ip range and http://mediaserver/y to another. I really don't care what WAN address they have, it's an intranet.

My pac file works just fine.

What do I need to do to get the same functionality in a web script that will work on win32, OSX and sun machines?

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
  • Why would you want to do this client side? The server receiving the request can determine the IP of the requesting client and serve either `x` or `y` as appropriate to your predefined IP address ranges, and it doesn't have to worry about which platform or which browser. – Stephen P Dec 01 '11 at 20:47
  • I had considered doing this server side but I only have control of the client code. Getting anything done server side will cause me a world of pain. I only ask because the futile attempt to do it client side seems to be happily implemented for the little used and completely non-standard wpad/pac format. I totally understand a server side implementation for identifying external ips for localisation etc. I'm on an intranet, where I want to deliver hi-res video to my Gb LAN and lo-res to the WAN on poor MPLS links; both easily identifiable by local IP. – Neil Martin Dec 05 '11 at 23:28

1 Answers1

1

There is no way that a machine locally can determine its own IP address as seen by another server. For example my Linux Laptop has 3 different valid IP addresses, and that is not including any NAT addresses which may be transiently assigned for external traffic. Getting Javascript to locally work out which one is "correct" is a futile task.

What you need is a redirect server (for example http://mediaserver/entrypoint-for-redirect) , which determine which IP address a request is coming from and then base on that redirect (HTTP-302) to the desired resource -- that being either http://mediaserver/x or y.

EDIT

As you are using apache, you may also be able to implement the same without a redirect using the mod-rewrite module -- I.e. install mod-rewrite on the server and create a rule which switches the traffic without a redirect to the right resource on the server.

Soren
  • 14,402
  • 4
  • 41
  • 67
  • thats exactly the answer i've been finding on the net. how does the .pac file javascript manage it on the fly without any problem. – Neil Martin Dec 01 '11 at 21:35
  • sorry soren. i answered no to your reply. not because its not valid or indeed useful, just that i know its possible. the proxy.pac file manages it just fine. what is so magical about it that the exact same myIpAddress() function fails when using it in a page script??? – Neil Martin Dec 01 '11 at 21:38
  • sorry to revalidate. i am running a simple apache server, internally, serving internal, intranet pages. i do not want to set up redirects on apache. i will do if i really have to. im just looking at this really funky, works 100% function that seems only to work within the pac file – Neil Martin Dec 01 '11 at 21:42
  • Looking at http://en.wikipedia.org/wiki/Proxy_auto-config it say _Web Proxy Autodiscovery Protocol (WPAD): Let the browser guess the location of the PAC file through DHCP and DNS lookups._ -- in effect you get an appropriate pac file depending on where you are coming _from_ . – Soren Dec 03 '11 at 02:26
  • DNS lookup provides the IPs for the servers you are connecting to, and DNS can provide different ips depending on the origin of the request, and the rules in the pac file determin the right routing based on the destination IP -- at no time does the pac file specify the laptops own IP address -- that is determined on the servers providing the pac file. – Soren Dec 03 '11 at 02:26
  • You could also try to use mod_rewrite on Apache. – Soren Dec 03 '11 at 14:39